## Loading packages 

library(lavaan)
library(OrthoPanels)
library(semTools)
library(foreign)
library(dplyr)
library(readstata13)

## Load in the data
dat <- read.dta13("C:/Replication files_Authoritarianism, Political Attitudes and Vote Choice/RICLPM data.dta")

#### MEASUREMENT MODELS ####

# Authoritarianism

auth_MM <- 
  "
auth7 =~ auth1_W7 + auth2_W7 + auth3_W7 + auth4_W7
auth10 =~ auth1_W10 + auth2_W10 + auth3_W10 + auth4_W10
auth11 =~ auth1_W11 + auth2_W11 + auth3_W11 + auth4_W11
auth14 =~ auth1_W14 + auth2_W14 + auth3_W14 + auth4_W14
auth10 ~ auth7
auth11 ~ auth10
auth14 ~ auth11
"
auth_MM_fit <- sem(auth_MM, 
                   test="Satorra-Bentler",
                   ordered = c("auth1_W7",
                               "auth2_W7",
                               "auth3_W7",
                               "auth4_W7",
                               "auth1_W10",
                               "auth2_W10",
                               "auth3_W10",
                               "auth4_W10",
                               "auth1_W11",
                               "auth2_W11",
                               "auth3_W11",
                               "auth4_W11",
                               "auth1_W14",
                               "auth2_W14",
                               "auth3_W14",
                               "auth4_W14"),
                   data=dat)
reliability(auth_MM_fit, return.total = TRUE)
fitMeasures(auth_MM_fit, c("chisq.scaled", "df", "pvalue.scaled", "cfi", "tli", "rmsea", "srmr", "aic"))
auth_MM_results <- parameterEstimates(auth_MM_fit)


auth_MM_2 <- 
  "
auth7 =~ auth1_W7 + auth2_W7 + auth3_W7 + auth4_W7
auth10 =~ auth1_W10 + auth2_W10 + auth3_W10 + auth4_W10
auth11 =~ auth1_W11 + auth2_W11 + auth3_W11 + auth4_W11
auth14 =~ auth1_W14 + auth2_W14 + auth3_W14 + auth4_W14
auth1_W7 ~~ auth1_W10 + auth1_W11 + auth1_W14
auth1_W10 ~~ auth1_W11 + auth1_W14
auth1_W11 ~~ auth1_W14
auth2_W7 ~~ auth2_W10 + auth2_W11 + auth2_W14
auth2_W10 ~~ auth2_W11 + auth2_W14
auth2_W11 ~~ auth2_W14
auth3_W7 ~~ auth3_W10 + auth3_W11 + auth3_W14
auth3_W10 ~~ auth3_W11 + auth3_W14
auth3_W11 ~~ auth3_W14
auth4_W7 ~~ auth4_W10 + auth4_W11 + auth4_W14
auth4_W10 ~~ auth4_W11 + auth4_W14
auth4_W11 ~~ auth4_W14

auth10 ~ auth7
auth11 ~ auth10
auth14 ~ auth11

"
auth_MM_2_fit <- sem(auth_MM_2, 
                     test="Satorra-Bentler",
                     ordered = c("auth1_W7",
                                 "auth2_W7",
                                 "auth3_W7",
                                 "auth4_W7",
                                 "auth1_W10",
                                 "auth2_W10",
                                 "auth3_W10",
                                 "auth4_W10",
                                 "auth1_W11",
                                 "auth2_W11",
                                 "auth3_W11",
                                 "auth4_W11",
                                 "auth1_W14",
                                 "auth2_W14",
                                 "auth3_W14",
                                 "auth4_W14"),
                     data=dat)
reliability(auth_MM_2_fit, return.total = TRUE)
summary(auth_MM_2_fit)
fitMeasures(auth_MM_2_fit, c("chisq.scaled", "df", "pvalue.scaled", "cfi", "tli", "rmsea", "srmr", "aic"))
auth_MM_2_results <- parameterEstimates(auth_MM_2_fit)

anova(auth_MM_fit, auth_MM_2_fit)

## Immigration

imm_pref_2_MM <- 
  "
imm7 =~ im_culturalW7 + immigSelf01W7
imm10 =~ im_culturalW10 + immigSelf01W10
imm11 =~ im_culturalW11 + immigSelf01W11
imm14 =~ im_culturalW14 + immigSelf01W14
imm10 ~ imm7
imm11 ~ imm10
imm14 ~ imm11
"
imm_pref_2_MM_fit <- sem(imm_pref_2_MM, 
                         test="Satorra-Bentler",
                         missing = "ML",
                         data=dat)
reliability(imm_pref_2_MM_fit, return.total = TRUE)
summary(imm_pref_2_MM_fit)
fitMeasures(imm_pref_2_MM_fit, c("chisq.scaled", "df", "pvalue.scaled", "cfi", "tli", "rmsea", "srmr", "aic"))

## Redistribution

redist_MM <- 
  "
red7 =~ 1*redistSelf01W7
red10 =~ 1*redistSelf01W10
red11 =~ 1*redistSelf01W11
red14 =~ 1*redistSelf01W14
red10 ~ red7
red11 ~ red10
red14 ~ red11
"
redist_MM_fit <- sem(redist_MM, se="robust.huber.white", test="Satorra-Bentler", data=dat)
reliability(redist_MM_fit, treturn.total = TRUE)
reliabilityL2(redist_MM_fit, "red7")
reliabilityL2(redist_MM_fit, "red10")
reliabilityL2(redist_MM_fit, "red11")
reliabilityL2(redist_MM_fit, "red14")

summary(redist_MM_fit)

## EU integration

eu_int_MM <- "
EU7 =~ eu_intW7
EU10 =~ eu_intW10
EU11 =~ eu_intW11
EU14 =~ eu_intW14
EU10 ~ EU7
EU11 ~ EU10
EU14 ~ EU11
"
eu_int_MM_fit <- sem(eu_int_MM, 
                     test="Satorra-Bentler", 
                     data=dat)
reliability(eu_int_MM_fit, return.total = TRUE)
reliabilityL2(eu_int_MM_fit, "EU7")
reliabilityL2(eu_int_MM_fit, "EU10")
reliabilityL2(eu_int_MM_fit, "EU11")
reliabilityL2(eu_int_MM_fit, "EU14")
summary(eu_int_MM_fit)

#### Measurement invariance ####

## Fitting the models 

configural <- '
  RIauth =~ 1*auth01W7 + 1*auth01W10 + 1*auth01W11 + 1*auth01W14
  RIim_prefW =~ 1*im_pref_2W7 + 1*im_pref_2W10 + 1*im_pref_2W11 + 1*im_pref_2W14  

  # Create within-person parts
  wauth01W7 =~ a*auth01W7
  wauth01W10 =~ a*auth01W10
  wauth01W11 =~ a*auth01W11 
  wauth01W14 =~ a*auth01W14
  
  wim_prefW7 =~ b*im_pref_2W7
  wim_prefW10 =~ b*im_pref_2W10
  wim_prefW11 =~ b*im_pref_2W11
  wim_prefW14 =~ b*im_pref_2W14
  
   # Estimate the lagged effects between the within-person centered variables.
  wauth01W10 + wim_prefW10 ~ wauth01W7 + wim_prefW7
  wauth01W11 + wim_prefW11 ~ wauth01W10 + wim_prefW10
  wauth01W14 + wim_prefW14 ~ wauth01W11 + wim_prefW11

  # Estimate the covariance between the within-person centered variables
  wauth01W7 ~~ wim_prefW7 # Covariance
  wauth01W10 ~~ wim_prefW10
  wauth01W11 ~~ wim_prefW11
  wauth01W14 ~~ wim_prefW14
  
# Constrain cov of between factors and exogenous within factors to 0:
  RIauth + RIim_prefW ~~ 0*wauth01W7 + 0*wim_prefW7
  
    # Fix error variance of indicators to (1 - alpha)*var
auth01W7 ~~ 0.02*auth01W7
auth01W10 ~~ 0.016*auth01W10
auth01W11 ~~ 0.018*auth01W11
auth01W14 ~~ 0.019*auth01W14
im_pref_2W7 ~~ 0.0147*im_pref_2W7
im_pref_2W10 ~~ 0.000155*im_pref_2W10
im_pref_2W11 ~~ 0.000147*im_pref_2W11
im_pref_2W14 ~~ 0.000512*im_pref_2W14

  '
configural.fit <- cfa(configural, 
                      data = dat, 
                      missing = 'ML',
                      estimator = "MLR")
summary(configural.fit, standardized = T)


weak_fa <- '
  RIauth =~ 1*auth01W7 + 1*auth01W10 + 1*auth01W11 + 1*auth01W14
  RIim_prefW =~ 1*im_pref_2W7 + 1*im_pref_2W10 + 1*im_pref_2W11 + 1*im_pref_2W14  

  # Create within-person parts
  wauth01W7 =~ 1*auth01W7
  wauth01W10 =~ 1*auth01W10
  wauth01W11 =~ 1*auth01W11 
  wauth01W14 =~ 1*auth01W14
  
  wim_prefW7 =~ 1*im_pref_2W7
  wim_prefW10 =~ 1*im_pref_2W10
  wim_prefW11 =~ 1*im_pref_2W11
  wim_prefW14 =~ 1*im_pref_2W14
  
   # Estimate the lagged effects between the within-person centered variables.
  wauth01W10 + wim_prefW10 ~ wauth01W7 + wim_prefW7
  wauth01W11 + wim_prefW11 ~ wauth01W10 + wim_prefW10
  wauth01W14 + wim_prefW14 ~ wauth01W11 + wim_prefW11

  # Estimate the covariance between the within-person centered variables
  wauth01W7 ~~ wim_prefW7 # Covariance
  wauth01W10 ~~ wim_prefW10
  wauth01W11 ~~ wim_prefW11
  wauth01W14 ~~ wim_prefW14
  
# Constrain cov of between factors and exogenous within factors to 0:
  RIauth + RIim_prefW ~~ 0*wauth01W7 + 0*wim_prefW7
  
    # Fix error variance of indicators to (1 - alpha)*var
auth01W7 ~~ 0.02*auth01W7
auth01W10 ~~ 0.016*auth01W10
auth01W11 ~~ 0.018*auth01W11
auth01W14 ~~ 0.019*auth01W14
im_pref_2W7 ~~ 0.0147*im_pref_2W7
im_pref_2W10 ~~ 0.000155*im_pref_2W10
im_pref_2W11 ~~ 0.000147*im_pref_2W11
im_pref_2W14 ~~ 0.000512*im_pref_2W14

  '
weak_fa.fit <- cfa(weak_fa, 
                   data = dat, 
                   missing = 'ML',
                   estimator = "MLR")
summary(weak_fa.fit, standardized = T)

strong_fa <- '
  RIauth =~ 1*auth01W7 + 1*auth01W10 + 1*auth01W11 + 1*auth01W14
  RIim_prefW =~ 1*im_pref_2W7 + 1*im_pref_2W10 + 1*im_pref_2W11 + 1*im_pref_2W14  

  # Create within-person parts
  wauth01W7 =~ 1*auth01W7
  wauth01W10 =~ 1*auth01W10
  wauth01W11 =~ 1*auth01W11 
  wauth01W14 =~ 1*auth01W14
  
  wim_prefW7 =~ 1*im_pref_2W7
  wim_prefW10 =~ 1*im_pref_2W10
  wim_prefW11 =~ 1*im_pref_2W11
  wim_prefW14 =~ 1*im_pref_2W14
  
  # Constrained intercepts over time
  auth01W7 + auth01W10 + auth01W11 + auth01W14 ~ c*1
  im_pref_2W7 + im_pref_2W10 + im_pref_2W11 + im_pref_2W14 ~ d*1 
  
  # Free latent means from t = 2 onward
  wauth01W10 + wauth01W11 + wauth01W14 + wim_prefW10 + wim_prefW11 + wim_prefW14 ~ 1
  
   # Estimate the lagged effects between the within-person centered variables.
  wauth01W10 + wim_prefW10 ~ wauth01W7 + wim_prefW7
  wauth01W11 + wim_prefW11 ~ wauth01W10 + wim_prefW10
  wauth01W14 + wim_prefW14 ~ wauth01W11 + wim_prefW11

  # Estimate the covariance between the within-person centered variables
  wauth01W7 ~~ wim_prefW7 # Covariance
  wauth01W10 ~~ wim_prefW10
  wauth01W11 ~~ wim_prefW11
  wauth01W14 ~~ wim_prefW14
  
# Constrain cov of between factors and exogenous within factors to 0:
  RIauth + RIim_prefW ~~ 0*wauth01W7 + 0*wim_prefW7
  
    # Fix error variance of indicators to (1 - alpha)*var
auth01W7 ~~ 0.02*auth01W7
auth01W10 ~~ 0.016*auth01W10
auth01W11 ~~ 0.018*auth01W11
auth01W14 ~~ 0.019*auth01W14
im_pref_2W7 ~~ 0.0147*im_pref_2W7
im_pref_2W10 ~~ 0.000155*im_pref_2W10
im_pref_2W11 ~~ 0.000147*im_pref_2W11
im_pref_2W14 ~~ 0.000512*im_pref_2W14

  '
strong_fa.fit <- cfa(strong_fa, 
                     data = dat, 
                     missing = 'ML',
                     estimator = "MLR")
summary(strong_fa.fit, standardized = T)

# Comparing model fit
fitmeasures(configural.fit)
fitmeasures(weak_fa.fit)
fitmeasures(strong_fa.fit)

#### CL2PMs ####

## Immigration

immig_pref_2_CL2PM <- '
   # Create between components (random intercepts)
  RIauth01W  =~ 1*std_auth01W7 + 1*std_auth01W10 + 1*std_auth01W11 + 1*std_auth01W14 
  RIim_prefW =~ 1*std_im_pref_2W7 + 1*std_im_pref_2W10 + 1*std_im_pref_2W11 + 1*std_im_pref_2W14 
  
  # Create within-person centered variables
  wauth01W7 =~ 1*std_auth01W7
  wauth01W10 =~ 1*std_auth01W10
  wauth01W11 =~ 1*std_auth01W11 
  wauth01W14 =~ 1*std_auth01W14

  wim_prefW7 =~ 1*std_im_pref_2W7
  wim_prefW10 =~ 1*std_im_pref_2W10
  wim_prefW11 =~ 1*std_im_pref_2W11
  wim_prefW14 =~ 1*std_im_pref_2W14

  # Estimate the lagged effects between the within-person centered variables.
  wauth01W10 + wim_prefW10 ~ wauth01W7 + wim_prefW7
  wauth01W11 + wim_prefW11 ~ wauth01W10 + wim_prefW10 + wauth01W7 + wim_prefW7
  wauth01W14 + wim_prefW14 ~ wauth01W11 + wim_prefW11 + wauth01W10 + wim_prefW10

  # Estimate the covariance between the within-person centered variables at the first wave. 
  wauth01W7 ~~ wim_prefW7 # Covariance
  
  # Estimate the covariances between the residuals of the within-person centered variables (the innovations).
  wauth01W10 ~~ wim_prefW10
  wauth01W11 ~~ wim_prefW11
  wauth01W14 ~~ wim_prefW14

  # Estimate the (residual) variance of the within-person centered variables.
  wauth01W7 ~~ wauth01W7 # Variances
  wim_prefW7 ~~ wim_prefW7 
  wauth01W10 ~~ wauth01W10 # Residual variances
  wim_prefW10 ~~ wim_prefW10 
  wauth01W11 ~~ wauth01W11
  wim_prefW11 ~~ wim_prefW11 
  wauth01W14 ~~ wauth01W14 
  wim_prefW14 ~~ wim_prefW14 
  
  # Fix the variance and covariance of the random intercepts to 0:
  RIauth01W ~~ 0*RIauth01W
  RIim_prefW ~~ 0*RIim_prefW
  RIauth01W ~~ 0*RIim_prefW
  
  '
immig_pref_2_CL2PM.fit <- lavaan(immig_pref_2_CL2PM, data = dat, 
                                 estimator = "MLR",
                                 missing = 'ML', meanstructure = T, int.ov.free = T) 
summary(immig_pref_2_CL2PM.fit, standardized = T)


## Redistribution

std_redist_pref_CL2PM <- '
  # Create between components (random intercepts)
  RIstd_auth01W  =~ 1*std_auth01W7 + 1*std_auth01W10 + 1*std_auth01W11 + 1*std_auth01W14 
  RIstd_redist_prefW =~ 1*std_redistSelf01W7 + 1*std_redistSelf01W10 + 1*std_redistSelf01W11 + 1*std_redistSelf01W14 
  
  # Create within-person centered variables
  wstd_auth01W7 =~ 1*std_auth01W7
  wstd_auth01W10 =~ 1*std_auth01W10
  wstd_auth01W11 =~ 1*std_auth01W11 
  wstd_auth01W14 =~ 1*std_auth01W14

  wstd_redist_prefW7 =~ 1*std_redistSelf01W7
  wstd_redist_prefW10 =~ 1*std_redistSelf01W10
  wstd_redist_prefW11 =~ 1*std_redistSelf01W11
  wstd_redist_prefW14 =~ 1*std_redistSelf01W14

  # Estimate the lagged effects between the within-person centered variables.
  wstd_auth01W10 + wstd_redist_prefW10 ~ wstd_auth01W7 + wstd_redist_prefW7
  wstd_auth01W11 + wstd_redist_prefW11 ~ wstd_auth01W10 + wstd_redist_prefW10 + wstd_auth01W7 + wstd_redist_prefW7
  wstd_auth01W14 + wstd_redist_prefW14 ~ wstd_auth01W11 + wstd_redist_prefW11 + wstd_auth01W10 + wstd_redist_prefW10

  # Estimate the covariance between the within-person centered variables at the first wave. 
  wstd_auth01W7 ~~ wstd_redist_prefW7 # Covariance
  
  # Estimate the covariances between the residuals of the within-person centered variables (the innovations).
  wstd_auth01W10 ~~ wstd_redist_prefW10
  wstd_auth01W11 ~~ wstd_redist_prefW11
  wstd_auth01W14 ~~ wstd_redist_prefW14
  

  # Estimate the (residual) variance of the within-person centered variables.
  wstd_auth01W7 ~~ wstd_auth01W7 # Variances
  wstd_redist_prefW7 ~~ wstd_redist_prefW7 
  wstd_auth01W10 ~~ wstd_auth01W10 # Residual variances
  wstd_redist_prefW10 ~~ wstd_redist_prefW10 
  wstd_auth01W11 ~~ wstd_auth01W11
  wstd_redist_prefW11 ~~ wstd_redist_prefW11 
  wstd_auth01W14 ~~ wstd_auth01W14 
  wstd_redist_prefW14 ~~ wstd_redist_prefW14 
  
  # Constrain the variance and covariance of the random intercepts to zero
  RIstd_auth01W ~~ 0*RIstd_auth01W
  RIstd_redist_prefW ~~ 0*RIstd_redist_prefW
  RIstd_auth01W ~~ 0*RIstd_redist_prefW

'

std_redist_pref_CL2PM.fit <- lavaan(std_redist_pref_CL2PM, data = dat, 
                                    estimator = "MLR",
                                    missing = 'ML', meanstructure = T, int.ov.free = T) 
summary(std_redist_pref_CL2PM.fit, standardized = T)

standardizedsolution(std_redist_pref_CL2PM.fit)

## EU integration

std_eu_pref_CL2PM <- '
  # Create between components (random intercepts)
  RIstd_auth01W  =~ 1*std_auth01W7 + 1*std_auth01W10 + 1*std_auth01W11 + 1*std_auth01W14 
  RIstd_eu_intW =~ 1*std_eu_intW7 + 1*std_eu_intW10 + 1*std_eu_intW11 + 1*std_eu_intW14 
  
  # Create within-person centered variables
  wstd_auth01W7 =~ 1*std_auth01W7
  wstd_auth01W10 =~ 1*std_auth01W10
  wstd_auth01W11 =~ 1*std_auth01W11 
  wstd_auth01W14 =~ 1*std_auth01W14

  wstd_eu_intW7 =~ 1*std_eu_intW7
  wstd_eu_intW10 =~ 1*std_eu_intW10
  wstd_eu_intW11 =~ 1*std_eu_intW11
  wstd_eu_intW14 =~ 1*std_eu_intW14

  # Estimate the lagged effects between the within-person centered variables.
  wstd_auth01W10 + wstd_eu_intW10 ~ wstd_auth01W7 + wstd_eu_intW7
  wstd_auth01W11 + wstd_eu_intW11 ~ wstd_auth01W10 + wstd_eu_intW10 + wstd_auth01W7 + wstd_eu_intW7
  wstd_auth01W14 + wstd_eu_intW14 ~ wstd_auth01W11 + wstd_eu_intW11 + wstd_auth01W10 + wstd_eu_intW10

  # Estimate the covariance between the within-person centered variables at the first wave. 
  wstd_auth01W7 ~~ wstd_eu_intW7 # Covariance
  
  # Estimate the covariances between the residuals of the within-person centered variables (the innovations).
  wstd_auth01W10 ~~ wstd_eu_intW10
  wstd_auth01W11 ~~ wstd_eu_intW11
  wstd_auth01W14 ~~ wstd_eu_intW14
  
  
  # Constrain the variance and covariance of the random intercepts to zero 
  RIstd_auth01W ~~ 0*RIstd_auth01W
  RIstd_eu_intW ~~ 0*RIstd_eu_intW
  RIstd_auth01W ~~ 0*RIstd_eu_intW

  # Estimate the (residual) variance of the within-person centered variables.
  wstd_auth01W7 ~~ wstd_auth01W7 # Variances
  wstd_eu_intW7 ~~ wstd_eu_intW7 
  wstd_auth01W10 ~~ wstd_auth01W10 # Residual variances
  wstd_eu_intW10 ~~ wstd_eu_intW10 
  wstd_auth01W11 ~~ wstd_auth01W11
  wstd_eu_intW11 ~~ wstd_eu_intW11 
  wstd_auth01W14 ~~ wstd_auth01W14 
  wstd_eu_intW14 ~~ wstd_eu_intW14 
  

'

std_eu_pref_CL2PM.fit <- lavaan(std_eu_pref_CL2PM, data = dat, 
                                estimator = "MLR",
                                missing = 'ML', meanstructure = T, int.ov.free = T) 
summary(std_eu_pref_CL2PM.fit, standardized = T)

standardizedsolution(std_eu_pref_CL2PM.fit)

#### RI-CLPMs - baseline models ####

# Immigration


std_immig_pref_b <- '
  # Create between components (random intercepts)
  RIauth01W  =~ 1*std_auth01W7 + 1*std_auth01W10 + 1*std_auth01W11 + 1*std_auth01W14 
  RIim_prefW =~ 1*std_im_pref_2W7 + 1*std_im_pref_2W10 + 1*std_im_pref_2W11 + 1*std_im_pref_2W14 
  
  # Create within-person centered variables
  wauth01W7 =~ 1*std_auth01W7
  wauth01W10 =~ 1*std_auth01W10
  wauth01W11 =~ 1*std_auth01W11 
  wauth01W14 =~ 1*std_auth01W14

  wim_prefW7 =~ 1*std_im_pref_2W7
  wim_prefW10 =~ 1*std_im_pref_2W10
  wim_prefW11 =~ 1*std_im_pref_2W11
  wim_prefW14 =~ 1*std_im_pref_2W14

  # Estimate the lagged effects between the within-person centered variables.
  wauth01W10 + wim_prefW10 ~ wauth01W7 + wim_prefW7
  wauth01W11 + wim_prefW11 ~ wauth01W10 + wim_prefW10
  wauth01W14 + wim_prefW14 ~ wauth01W11 + wim_prefW11

  # Estimate the covariance between the within-person centered variables at the first wave. 
  wauth01W7 ~~ wim_prefW7 # Covariance
  
  # Estimate the covariances between the residuals of the within-person centered variables (the innovations).
  wauth01W10 ~~ wim_prefW10
  wauth01W11 ~~ wim_prefW11
  wauth01W14 ~~ wim_prefW14
  
  
  # Estimate the variance and covariance of the random intercepts. 
  RIauth01W ~~ RIauth01W
  RIim_prefW ~~ RIim_prefW
  RIauth01W ~~ RIim_prefW

  # Estimate the (residual) variance of the within-person centered variables.
  wauth01W7 ~~ wauth01W7 # Variances
  wim_prefW7 ~~ wim_prefW7 
  wauth01W10 ~~ wauth01W10 # Residual variances
  wim_prefW10 ~~ wim_prefW10 
  wauth01W11 ~~ wauth01W11
  wim_prefW11 ~~ wim_prefW11 
  wauth01W14 ~~ wauth01W14 
  wim_prefW14 ~~ wim_prefW14 
  
# Fix error variance of indicators to (1 - alpha)*var
std_auth01W7 ~~ 0.02*std_auth01W7
std_auth01W10 ~~ 0.016*std_auth01W10
std_auth01W11 ~~ 0.018*std_auth01W11
std_auth01W14 ~~ 0.019*std_auth01W14
std_im_pref_2W7 ~~ 0.0147*std_im_pref_2W7
std_im_pref_2W10 ~~ 0.000155*std_im_pref_2W10
std_im_pref_2W11 ~~ 0.000147*std_im_pref_2W11
std_im_pref_2W14 ~~ 0.000512*std_im_pref_2W14

  '

std_immig_pref_b.fit <- lavaan(std_immig_pref_b, data = dat, 
                             estimator = "MLR",
                             missing = 'ML', meanstructure = T, int.ov.free = T) 
summary(std_immig_pref_b.fit, standardized = T)


# Redistribution 

std_redist_pref_b <- '
  # Create between components (random intercepts)
  RIstd_auth01W  =~ 1*std_auth01W7 + 1*std_auth01W10 + 1*std_auth01W11 + 1*std_auth01W14 
  RIstd_redist_prefW =~ 1*std_redistSelf01W7 + 1*std_redistSelf01W10 + 1*std_redistSelf01W11 + 1*std_redistSelf01W14 
  
  # Create within-person centered variables
  wstd_auth01W7 =~ 1*std_auth01W7
  wstd_auth01W10 =~ 1*std_auth01W10
  wstd_auth01W11 =~ 1*std_auth01W11 
  wstd_auth01W14 =~ 1*std_auth01W14

  wstd_redist_prefW7 =~ 1*std_redistSelf01W7
  wstd_redist_prefW10 =~ 1*std_redistSelf01W10
  wstd_redist_prefW11 =~ 1*std_redistSelf01W11
  wstd_redist_prefW14 =~ 1*std_redistSelf01W14

  # Estimate the lagged effects between the within-person centered variables.
  wstd_auth01W10 + wstd_redist_prefW10 ~ wstd_auth01W7 + wstd_redist_prefW7
  wstd_auth01W11 + wstd_redist_prefW11 ~ wstd_auth01W10 + wstd_redist_prefW10
  wstd_auth01W14 + wstd_redist_prefW14 ~ wstd_auth01W11 + wstd_redist_prefW11

  # Estimate the covariance between the within-person centered variables at the first wave. 
  wstd_auth01W7 ~~ wstd_redist_prefW7 # Covariance
  
  # Estimate the covariances between the residuals of the within-person centered variables (the innovations).
  wstd_auth01W10 ~~ wstd_redist_prefW10
  wstd_auth01W11 ~~ wstd_redist_prefW11
  wstd_auth01W14 ~~ wstd_redist_prefW14
  
  
  # Estimate the variance and covariance of the random intercepts. 
  RIstd_auth01W ~~ RIstd_auth01W
  RIstd_redist_prefW ~~ RIstd_redist_prefW
  RIstd_auth01W ~~ RIstd_redist_prefW

  # Estimate the (residual) variance of the within-person centered variables.
  wstd_auth01W7 ~~ wstd_auth01W7 # Variances
  wstd_redist_prefW7 ~~ wstd_redist_prefW7 
  wstd_auth01W10 ~~ wstd_auth01W10 # Residual variances
  wstd_redist_prefW10 ~~ wstd_redist_prefW10 
  wstd_auth01W11 ~~ wstd_auth01W11
  wstd_redist_prefW11 ~~ wstd_redist_prefW11 
  wstd_auth01W14 ~~ wstd_auth01W14 
  wstd_redist_prefW14 ~~ wstd_redist_prefW14 
  
  # Fix error variance of indicators to (1 - alpha)*var
std_auth01W10 ~~ 0.0058*std_auth01W10
std_auth01W11 ~~ 0.002*std_auth01W11
std_auth01W14 ~~ 0.00725*std_auth01W14
std_redistSelf01W10 ~~ 0.01038*std_redistSelf01W10
std_redistSelf01W11 ~~ 0.00933*std_redistSelf01W11
std_redistSelf01W14 ~~ 0*std_redistSelf01W14

'

std_redist_pref_b.fit <- lavaan(std_redist_pref_b, data = dat, 
                                estimator = "MLR",
                                missing = 'ML', meanstructure = T, int.ov.free = T) 
summary(std_redist_pref_b.fit, standardized = T)

fitmeasures(std_redist_pref_b.fit)

# EU integration 

std_eu_pref_b <- '
  # Create between components (random intercepts)
  RIstd_auth01W  =~ 1*std_auth01W7 + 1*std_auth01W10 + 1*std_auth01W11 + 1*std_auth01W14 
  RIstd_eu_intW =~ 1*std_eu_intW7 + 1*std_eu_intW10 + 1*std_eu_intW11 + 1*std_eu_intW14 
  
  # Create within-person centered variables
  wstd_auth01W7 =~ 1*std_auth01W7
  wstd_auth01W10 =~ 1*std_auth01W10
  wstd_auth01W11 =~ 1*std_auth01W11 
  wstd_auth01W14 =~ 1*std_auth01W14

  wstd_eu_intW7 =~ 1*std_eu_intW7
  wstd_eu_intW10 =~ 1*std_eu_intW10
  wstd_eu_intW11 =~ 1*std_eu_intW11
  wstd_eu_intW14 =~ 1*std_eu_intW14

  # Estimate the lagged effects between the within-person centered variables.
  wstd_auth01W10 + wstd_eu_intW10 ~ wstd_auth01W7 + wstd_eu_intW7
  wstd_auth01W11 + wstd_eu_intW11 ~ wstd_auth01W10 + wstd_eu_intW10
  wstd_auth01W14 + wstd_eu_intW14 ~ wstd_auth01W11 + wstd_eu_intW11

  # Estimate the covariance between the within-person centered variables at the first wave. 
  wstd_auth01W7 ~~ wstd_eu_intW7 # Covariance
  
  # Estimate the covariances between the residuals of the within-person centered variables (the innovations).
  wstd_auth01W10 ~~ wstd_eu_intW10
  wstd_auth01W11 ~~ wstd_eu_intW11
  wstd_auth01W14 ~~ wstd_eu_intW14
  
  
  # Estimate the variance and covariance of the random intercepts. 
  RIstd_auth01W ~~ RIstd_auth01W
  RIstd_eu_intW ~~ RIstd_eu_intW
  RIstd_auth01W ~~ RIstd_eu_intW

  # Estimate the (residual) variance of the within-person centered variables.
  wstd_auth01W7 ~~ wstd_auth01W7 # Variances
  wstd_eu_intW7 ~~ wstd_eu_intW7 
  wstd_auth01W10 ~~ wstd_auth01W10 # Residual variances
  wstd_eu_intW10 ~~ wstd_eu_intW10 
  wstd_auth01W11 ~~ wstd_auth01W11
  wstd_eu_intW11 ~~ wstd_eu_intW11 
  wstd_auth01W14 ~~ wstd_auth01W14 
  wstd_eu_intW14 ~~ wstd_eu_intW14 
  
  # Fix error variance of indicators to (1 - alpha)*var
std_auth01W10 ~~ 0.0058*std_auth01W10
std_auth01W11 ~~ 0.002*std_auth01W11
std_auth01W14 ~~ 0.00725*std_auth01W14
std_eu_intW10 ~~ 0.0028*std_eu_intW10
std_eu_intW11 ~~ 0.00194*std_eu_intW11
std_eu_intW14 ~~ 0*std_eu_intW14


'

std_eu_pref_b.fit <- lavaan(std_eu_pref_b, data = dat, 
                            estimator = "MLR",
                            missing = 'ML', meanstructure = T, int.ov.free = T) 
summary(std_eu_pref_b.fit, standardized = T)



#### RI-CLPMs - TVC models ####

### Immigration

std_immig_pref_2_tvc <- '
  # Create between components (random intercepts)
  RIauth01W  =~ 1*std_auth01W7 + 1*std_auth01W10 + 1*std_auth01W11 + 1*std_auth01W14 
  RIim_prefW =~ 1*std_im_pref_2W7 + 1*std_im_pref_2W10 + 1*std_im_pref_2W11 + 1*std_im_pref_2W14 
  
  # Create within-person centered variables
  wauth01W7 =~ 1*std_auth01W7
  wauth01W10 =~ 1*std_auth01W10
  wauth01W11 =~ 1*std_auth01W11 
  wauth01W14 =~ 1*std_auth01W14

  wim_prefW7 =~ 1*std_im_pref_2W7
  wim_prefW10 =~ 1*std_im_pref_2W10
  wim_prefW11 =~ 1*std_im_pref_2W11
  wim_prefW14 =~ 1*std_im_pref_2W14

  # Estimate the lagged effects between the within-person centered variables.
  wauth01W10 + wim_prefW10 ~ wauth01W7 + wim_prefW7
  wauth01W11 + wim_prefW11 ~ wauth01W10 + wim_prefW10
  wauth01W14 + wim_prefW14 ~ wauth01W11 + wim_prefW11

  # Estimate the covariance between the within-person centered variables at the first wave. 
  wauth01W7 ~~ wim_prefW7 # Covariance
  
  # Estimate the covariances between the residuals of the within-person centered variables (the innovations).
  wauth01W10 ~~ wim_prefW10
  wauth01W11 ~~ wim_prefW11
  wauth01W14 ~~ wim_prefW14
  
  
  # Estimate the variance and covariance of the random intercepts. 
  RIauth01W ~~ RIauth01W
  RIim_prefW ~~ RIim_prefW
  RIauth01W ~~ RIim_prefW

  # Estimate the (residual) variance of the within-person centered variables.
  wauth01W7 ~~ wauth01W7 # Variances
  wim_prefW7 ~~ wim_prefW7 
  wauth01W10 ~~ wauth01W10 # Residual variances
  wim_prefW10 ~~ wim_prefW10 
  wauth01W11 ~~ wauth01W11
  wim_prefW11 ~~ wim_prefW11 
  wauth01W14 ~~ wauth01W14 
  wim_prefW14 ~~ wim_prefW14 
  
# Fix error variance of indicators to (1 - alpha)*var
std_auth01W7 ~~ 0.02*std_auth01W7
std_auth01W10 ~~ 0.016*std_auth01W10
std_auth01W11 ~~ 0.018*std_auth01W11
std_auth01W14 ~~ 0.019*std_auth01W14
std_im_pref_2W7 ~~ 0.0147*std_im_pref_2W7
std_im_pref_2W10 ~~ 0.000155*std_im_pref_2W10
std_im_pref_2W11 ~~ 0.000147*std_im_pref_2W11
std_im_pref_2W14 ~~ 0.000512*std_im_pref_2W14

# incorporate tvc - age
wauth01W7 + wim_prefW7 ~ ageW7
wauth01W10 + wim_prefW10 ~ ageW10
wauth01W11 + wim_prefW11 ~ ageW11
wauth01W14 + wim_prefW14 ~ ageW14

ageW7 ~~ ageW7
ageW10 ~~ ageW10
ageW11 ~~ ageW11
ageW14 ~~ ageW14

ageW7 ~~ ageW10 + ageW11 + ageW14
ageW10 ~~ ageW11 + ageW14
ageW11 ~~ ageW14

# incorporate tvc – social grade
wauth01W7 + wim_prefW7 ~ p_socgradeW7
wauth01W10 + wim_prefW10 ~ p_socgradeW10
wauth01W11 + wim_prefW11 ~ p_socgradeW11
wauth01W14 + wim_prefW14 ~ p_socgradeW14

p_socgradeW7 ~~ p_socgradeW7
p_socgradeW10 ~~ p_socgradeW10
p_socgradeW11 ~~ p_socgradeW11
p_socgradeW14 ~~ p_socgradeW14

p_socgradeW7 ~~ p_socgradeW10 + p_socgradeW11 + p_socgradeW14
p_socgradeW10 ~~ p_socgradeW11 + p_socgradeW14
p_socgradeW11 ~~ p_socgradeW14

# incorporate tvc – income
wauth01W7 + wim_prefW7 ~ incomerec7
wauth01W10 + wim_prefW10 ~ incomerec10
wauth01W11 + wim_prefW11 ~ incomerec11
wauth01W14 + wim_prefW14 ~ incomerec14

incomerec7 ~~ incomerec7
incomerec10 ~~ incomerec10
incomerec11 ~~ incomerec11
incomerec14 ~~ incomerec14

incomerec7 ~~ incomerec10 + incomerec11 + incomerec14
incomerec10 ~~ incomerec11 + incomerec14
incomerec11 ~~ incomerec14
  '
std_immig_pref_2_tvc.fit <- lavaan(std_immig_pref_2_tvc, 
                                   data = dat, 
                                   estimator = "MLR",
                                   missing = 'ML', 
                                   meanstructure = T, 
                                   int.ov.free = T) 
summary(std_immig_pref_2_tvc.fit, standardized = T)

# Redistribution

std_redist_pref_tvc <- '
  # Create between components (random intercepts)
  RIstd_auth01W  =~ 1*std_auth01W7 + 1*std_auth01W10 + 1*std_auth01W11 + 1*std_auth01W14 
  RIstd_redist_prefW =~ 1*std_redistSelf01W7 + 1*std_redistSelf01W10 + 1*std_redistSelf01W11 + 1*std_redistSelf01W14 
  
  # Create within-person centered variables
  wstd_auth01W7 =~ 1*std_auth01W7
  wstd_auth01W10 =~ 1*std_auth01W10
  wstd_auth01W11 =~ 1*std_auth01W11 
  wstd_auth01W14 =~ 1*std_auth01W14

  wstd_redist_prefW7 =~ 1*std_redistSelf01W7
  wstd_redist_prefW10 =~ 1*std_redistSelf01W10
  wstd_redist_prefW11 =~ 1*std_redistSelf01W11
  wstd_redist_prefW14 =~ 1*std_redistSelf01W14

  # Estimate the lagged effects between the within-person centered variables.
  wstd_auth01W10 + wstd_redist_prefW10 ~ wstd_auth01W7 + wstd_redist_prefW7
  wstd_auth01W11 + wstd_redist_prefW11 ~ wstd_auth01W10 + wstd_redist_prefW10
  wstd_auth01W14 + wstd_redist_prefW14 ~ wstd_auth01W11 + wstd_redist_prefW11

  # Estimate the covariance between the within-person centered variables at the first wave. 
  wstd_auth01W7 ~~ wstd_redist_prefW7 # Covariance
  
  # Estimate the covariances between the residuals of the within-person centered variables (the innovations).
  wstd_auth01W10 ~~ wstd_redist_prefW10
  wstd_auth01W11 ~~ wstd_redist_prefW11
  wstd_auth01W14 ~~ wstd_redist_prefW14
  
  
  # Estimate the variance and covariance of the random intercepts. 
  RIstd_auth01W ~~ RIstd_auth01W
  RIstd_redist_prefW ~~ RIstd_redist_prefW
  RIstd_auth01W ~~ RIstd_redist_prefW

  # Estimate the (residual) variance of the within-person centered variables.
  wstd_auth01W7 ~~ wstd_auth01W7 # Variances
  wstd_redist_prefW7 ~~ wstd_redist_prefW7 
  wstd_auth01W10 ~~ wstd_auth01W10 # Residual variances
  wstd_redist_prefW10 ~~ wstd_redist_prefW10 
  wstd_auth01W11 ~~ wstd_auth01W11
  wstd_redist_prefW11 ~~ wstd_redist_prefW11 
  wstd_auth01W14 ~~ wstd_auth01W14 
  wstd_redist_prefW14 ~~ wstd_redist_prefW14 
  
  # Fix error variance of indicators to (1 - alpha)*var
std_auth01W10 ~~ 0.0058*std_auth01W10
std_auth01W11 ~~ 0.002*std_auth01W11
std_auth01W14 ~~ 0.00725*std_auth01W14
std_redistSelf01W10 ~~ 0.01038*std_redistSelf01W10
std_redistSelf01W11 ~~ 0.00933*std_redistSelf01W11
std_redistSelf01W14 ~~ 0*std_redistSelf01W14

# incorporate tvc - age
wstd_auth01W7 + wstd_redist_prefW7 ~ ageW7
wstd_auth01W10 + wstd_redist_prefW10 ~ ageW10
wstd_auth01W11 + wstd_redist_prefW11 ~ ageW11
wstd_auth01W14 + wstd_redist_prefW14 ~ ageW14

ageW7 ~~ ageW7
ageW10 ~~ ageW10
ageW11 ~~ ageW11
ageW14 ~~ ageW14

ageW7 ~~ ageW10 + ageW11 + ageW14
ageW10 ~~ ageW11 + ageW14
ageW11 ~~ ageW14

# incorporate tvc – income
wstd_auth01W7 + wstd_redist_prefW7 ~ incomerec7
wstd_auth01W10 + wstd_redist_prefW10 ~ incomerec10
wstd_auth01W11 + wstd_redist_prefW11 ~ incomerec11
wstd_auth01W14 + wstd_redist_prefW14 ~ incomerec14

incomerec7 ~~ incomerec7
incomerec10 ~~ incomerec10
incomerec11 ~~ incomerec11
incomerec14 ~~ incomerec14

incomerec7 ~~ incomerec10 + incomerec11 + incomerec14
incomerec10 ~~ incomerec11 + incomerec14
incomerec11 ~~ incomerec14

# incorporate tvc – social grade
wstd_auth01W7 + wstd_redist_prefW7 ~ p_socgradeW7
wstd_auth01W10 + wstd_redist_prefW10 ~ p_socgradeW10
wstd_auth01W11 + wstd_redist_prefW11 ~ p_socgradeW11
wstd_auth01W14 + wstd_redist_prefW14 ~ p_socgradeW14

p_socgradeW7 ~~ p_socgradeW7
p_socgradeW10 ~~ p_socgradeW10
p_socgradeW11 ~~ p_socgradeW11
p_socgradeW14 ~~ p_socgradeW14

p_socgradeW7 ~~ p_socgradeW10 + p_socgradeW11 + p_socgradeW14
p_socgradeW10 ~~ p_socgradeW11 + p_socgradeW14
p_socgradeW11 ~~ p_socgradeW14


'

std_redist_pref_tvc.fit <- lavaan(std_redist_pref_tvc, data = dat, 
                                      estimator = "MLR",
                                      missing = 'ML', meanstructure = T, int.ov.free = T) 
summary(std_redist_pref_tvc.fit, standardized = T)

std_eu_pref_tvc <- '
  # Create between components (random intercepts)
  RIstd_auth01W  =~ 1*std_auth01W7 + 1*std_auth01W10 + 1*std_auth01W11 + 1*std_auth01W14 
  RIstd_eu_intW =~ 1*std_eu_intW7 + 1*std_eu_intW10 + 1*std_eu_intW11 + 1*std_eu_intW14 
  
  # Create within-person centered variables
  wstd_auth01W7 =~ 1*std_auth01W7
  wstd_auth01W10 =~ 1*std_auth01W10
  wstd_auth01W11 =~ 1*std_auth01W11 
  wstd_auth01W14 =~ 1*std_auth01W14

  wstd_eu_intW7 =~ 1*std_eu_intW7
  wstd_eu_intW10 =~ 1*std_eu_intW10
  wstd_eu_intW11 =~ 1*std_eu_intW11
  wstd_eu_intW14 =~ 1*std_eu_intW14

  # Estimate the lagged effects between the within-person centered variables.
  wstd_auth01W10 + wstd_eu_intW10 ~ wstd_auth01W7 + wstd_eu_intW7
  wstd_auth01W11 + wstd_eu_intW11 ~ wstd_auth01W10 + wstd_eu_intW10
  wstd_auth01W14 + wstd_eu_intW14 ~ wstd_auth01W11 + wstd_eu_intW11

  # Estimate the covariance between the within-person centered variables at the first wave. 
  wstd_auth01W7 ~~ wstd_eu_intW7 # Covariance
  
  # Estimate the covariances between the residuals of the within-person centered variables (the innovations).
  wstd_auth01W10 ~~ wstd_eu_intW10
  wstd_auth01W11 ~~ wstd_eu_intW11
  wstd_auth01W14 ~~ wstd_eu_intW14
  
  
  # Estimate the variance and covariance of the random intercepts. 
  RIstd_auth01W ~~ RIstd_auth01W
  RIstd_eu_intW ~~ RIstd_eu_intW
  RIstd_auth01W ~~ RIstd_eu_intW

  # Estimate the (residual) variance of the within-person centered variables.
  wstd_auth01W7 ~~ wstd_auth01W7 # Variances
  wstd_eu_intW7 ~~ wstd_eu_intW7 
  wstd_auth01W10 ~~ wstd_auth01W10 # Residual variances
  wstd_eu_intW10 ~~ wstd_eu_intW10 
  wstd_auth01W11 ~~ wstd_auth01W11
  wstd_eu_intW11 ~~ wstd_eu_intW11 
  wstd_auth01W14 ~~ wstd_auth01W14 
  wstd_eu_intW14 ~~ wstd_eu_intW14 
  
  # Fix error variance of indicators to (1 - alpha)*var
std_auth01W10 ~~ 0.0058*std_auth01W10
std_auth01W11 ~~ 0.002*std_auth01W11
std_auth01W14 ~~ 0.00725*std_auth01W14
std_eu_intW10 ~~ 0.0028*std_eu_intW10
std_eu_intW11 ~~ 0.00194*std_eu_intW11
std_eu_intW14 ~~ 0*std_eu_intW14

# incorporate tvc - age
wstd_auth01W7 + wstd_eu_intW7 ~ ageW7
wstd_auth01W10 + wstd_eu_intW10 ~ ageW10
wstd_auth01W11 + wstd_eu_intW11 ~ ageW11
wstd_auth01W14 + wstd_eu_intW14 ~ ageW14

ageW7 ~~ ageW7
ageW10 ~~ ageW10
ageW11 ~~ ageW11
ageW14 ~~ ageW14

ageW7 ~~ ageW10 + ageW11 + ageW14
ageW10 ~~ ageW11 + ageW14
ageW11 ~~ ageW14

# incorporate tvc – income
wstd_auth01W7 + wstd_eu_intW7 ~ incomerec7
wstd_auth01W10 + wstd_eu_intW10 ~ incomerec10
wstd_auth01W11 + wstd_eu_intW11 ~ incomerec11
wstd_auth01W14 + wstd_eu_intW14 ~ incomerec14

incomerec7 ~~ incomerec7
incomerec10 ~~ incomerec10
incomerec11 ~~ incomerec11
incomerec14 ~~ incomerec14

incomerec7 ~~ incomerec10 + incomerec11 + incomerec14
incomerec10 ~~ incomerec11 + incomerec14
incomerec11 ~~ incomerec14

# incorporate tvc – social grade
wstd_auth01W7 + wstd_eu_intW7 ~ p_socgradeW7
wstd_auth01W10 + wstd_eu_intW10 ~ p_socgradeW10
wstd_auth01W11 + wstd_eu_intW11 ~ p_socgradeW11
wstd_auth01W14 + wstd_eu_intW14 ~ p_socgradeW14

p_socgradeW7 ~~ p_socgradeW7
p_socgradeW10 ~~ p_socgradeW10
p_socgradeW11 ~~ p_socgradeW11
p_socgradeW14 ~~ p_socgradeW14

p_socgradeW7 ~~ p_socgradeW10 + p_socgradeW11 + p_socgradeW14
p_socgradeW10 ~~ p_socgradeW11 + p_socgradeW14
p_socgradeW11 ~~ p_socgradeW14

'

std_eu_pref_tvc.fit <- lavaan(std_eu_pref_tvc, data = dat, 
                                  estimator = "MLR",
                                  missing = 'ML', meanstructure = T, int.ov.free = T) 
summary(std_eu_pref_tvc.fit, standardized = T)

#### CLPMs - baseline models ####

# Immigration 

std_immig_pref_CLPM_b <- '
  # Create between components (random intercepts)
  RIauth01W  =~ 1*std_auth01W7 + 1*std_auth01W10 + 1*std_auth01W11 + 1*std_auth01W14 
  RIim_prefW =~ 1*std_im_pref_2W7 + 1*std_im_pref_2W10 + 1*std_im_pref_2W11 + 1*std_im_pref_2W14 
  
  # Create within-person centered variables
  wauth01W7 =~ 1*std_auth01W7
  wauth01W10 =~ 1*std_auth01W10
  wauth01W11 =~ 1*std_auth01W11 
  wauth01W14 =~ 1*std_auth01W14

  wim_prefW7 =~ 1*std_im_pref_2W7
  wim_prefW10 =~ 1*std_im_pref_2W10
  wim_prefW11 =~ 1*std_im_pref_2W11
  wim_prefW14 =~ 1*std_im_pref_2W14

  # Estimate the lagged effects between the within-person centered variables.
  wauth01W10 + wim_prefW10 ~ wauth01W7 + wim_prefW7
  wauth01W11 + wim_prefW11 ~ wauth01W10 + wim_prefW10
  wauth01W14 + wim_prefW14 ~ wauth01W11 + wim_prefW11

  # Estimate the covariance between the within-person centered variables at the first wave. 
  wauth01W7 ~~ wim_prefW7 # Covariance
  
  # Estimate the covariances between the residuals of the within-person centered variables (the innovations).
  wauth01W10 ~~ wim_prefW10
  wauth01W11 ~~ wim_prefW11
  wauth01W14 ~~ wim_prefW14
  
  
  # Fix the variance and covariance of the random intercepts to zero 
  RIauth01W ~~ 0*RIauth01W
  RIim_prefW ~~ 0*RIim_prefW
  RIauth01W ~~ 0*RIim_prefW

  # Estimate the (residual) variance of the within-person centered variables.
  wauth01W7 ~~ wauth01W7 # Variances
  wim_prefW7 ~~ wim_prefW7 
  wauth01W10 ~~ wauth01W10 # Residual variances
  wim_prefW10 ~~ wim_prefW10 
  wauth01W11 ~~ wauth01W11
  wim_prefW11 ~~ wim_prefW11 
  wauth01W14 ~~ wauth01W14 
  wim_prefW14 ~~ wim_prefW14 
  
# Fix error variance of indicators to (1 - alpha)*var
std_auth01W7 ~~ 0.02*std_auth01W7
std_auth01W10 ~~ 0.016*std_auth01W10
std_auth01W11 ~~ 0.018*std_auth01W11
std_auth01W14 ~~ 0.019*std_auth01W14
std_im_pref_2W7 ~~ 0.0147*std_im_pref_2W7
std_im_pref_2W10 ~~ 0.000155*std_im_pref_2W10
std_im_pref_2W11 ~~ 0.000147*std_im_pref_2W11
std_im_pref_2W14 ~~ 0.000512*std_im_pref_2W14

  '

std_immig_pref_CLPM_b.fit <- lavaan(std_immig_pref_CLPM_b,
                                data = dat, 
                                estimator = "MLR",
                                missing = 'ML', 
                                meanstructure = T, 
                                int.ov.free = T) 
summary(std_immig_pref_CLPM_b.fit, standardized = T)

# Redistribution

std_redist_pref_CLPM_b <- '
  # Create between components (random intercepts)
  RIstd_auth01W  =~ 1*std_auth01W7 + 1*std_auth01W10 + 1*std_auth01W11 + 1*std_auth01W14 
  RIstd_redist_prefW =~ 1*std_redistSelf01W7 + 1*std_redistSelf01W10 + 1*std_redistSelf01W11 + 1*std_redistSelf01W14 
  
  # Create within-person centered variables
  wstd_auth01W7 =~ 1*std_auth01W7
  wstd_auth01W10 =~ 1*std_auth01W10
  wstd_auth01W11 =~ 1*std_auth01W11 
  wstd_auth01W14 =~ 1*std_auth01W14

  wstd_redist_prefW7 =~ 1*std_redistSelf01W7
  wstd_redist_prefW10 =~ 1*std_redistSelf01W10
  wstd_redist_prefW11 =~ 1*std_redistSelf01W11
  wstd_redist_prefW14 =~ 1*std_redistSelf01W14

  # Estimate the lagged effects between the within-person centered variables.
  wstd_auth01W10 + wstd_redist_prefW10 ~ wstd_auth01W7 + wstd_redist_prefW7
  wstd_auth01W11 + wstd_redist_prefW11 ~ wstd_auth01W10 + wstd_redist_prefW10
  wstd_auth01W14 + wstd_redist_prefW14 ~ wstd_auth01W11 + wstd_redist_prefW11

  # Estimate the covariance between the within-person centered variables at the first wave. 
  wstd_auth01W7 ~~ wstd_redist_prefW7 # Covariance
  
  # Estimate the covariances between the residuals of the within-person centered variables (the innovations).
  wstd_auth01W10 ~~ wstd_redist_prefW10
  wstd_auth01W11 ~~ wstd_redist_prefW11
  wstd_auth01W14 ~~ wstd_redist_prefW14
  
  
  # Constrain the variance and covariance of the random intercepts to zero  
  RIstd_auth01W ~~ 0*RIstd_auth01W
  RIstd_redist_prefW ~~ 0*RIstd_redist_prefW
  RIstd_auth01W ~~ 0*RIstd_redist_prefW

  # Estimate the (residual) variance of the within-person centered variables.
  wstd_auth01W7 ~~ wstd_auth01W7 # Variances
  wstd_redist_prefW7 ~~ wstd_redist_prefW7 
  wstd_auth01W10 ~~ wstd_auth01W10 # Residual variances
  wstd_redist_prefW10 ~~ wstd_redist_prefW10 
  wstd_auth01W11 ~~ wstd_auth01W11
  wstd_redist_prefW11 ~~ wstd_redist_prefW11 
  wstd_auth01W14 ~~ wstd_auth01W14 
  wstd_redist_prefW14 ~~ wstd_redist_prefW14 
  
  # Fix error variance of indicators to (1 - alpha)*var
std_auth01W10 ~~ 0.0058*std_auth01W10
std_auth01W11 ~~ 0.002*std_auth01W11
std_auth01W14 ~~ 0.00725*std_auth01W14
std_redistSelf01W10 ~~ 0.01038*std_redistSelf01W10
std_redistSelf01W11 ~~ 0.00933*std_redistSelf01W11
std_redistSelf01W14 ~~ 0*std_redistSelf01W14

'

std_redist_pref_CLPM_b.fit <- lavaan(std_redist_pref_CLPM_b, data = dat, 
                                     estimator = "MLR",
                                     missing = 'ML', meanstructure = T, int.ov.free = T) 
summary(std_redist_pref_CLPM_b.fit, standardized = T)

# EU integration

std_eu_pref_CLPM_b <- '
  # Create between components (random intercepts)
  RIstd_auth01W  =~ 1*std_auth01W7 + 1*std_auth01W10 + 1*std_auth01W11 + 1*std_auth01W14 
  RIstd_eu_intW =~ 1*std_eu_intW7 + 1*std_eu_intW10 + 1*std_eu_intW11 + 1*std_eu_intW14 
  
  # Create within-person centered variables
  wstd_auth01W7 =~ 1*std_auth01W7
  wstd_auth01W10 =~ 1*std_auth01W10
  wstd_auth01W11 =~ 1*std_auth01W11 
  wstd_auth01W14 =~ 1*std_auth01W14

  wstd_eu_intW7 =~ 1*std_eu_intW7
  wstd_eu_intW10 =~ 1*std_eu_intW10
  wstd_eu_intW11 =~ 1*std_eu_intW11
  wstd_eu_intW14 =~ 1*std_eu_intW14

  # Estimate the lagged effects between the within-person centered variables.
  wstd_auth01W10 + wstd_eu_intW10 ~ wstd_auth01W7 + wstd_eu_intW7
  wstd_auth01W11 + wstd_eu_intW11 ~ wstd_auth01W10 + wstd_eu_intW10
  wstd_auth01W14 + wstd_eu_intW14 ~ wstd_auth01W11 + wstd_eu_intW11

  # Estimate the covariance between the within-person centered variables at the first wave. 
  wstd_auth01W7 ~~ wstd_eu_intW7 # Covariance
  
  # Estimate the covariances between the residuals of the within-person centered variables (the innovations).
  wstd_auth01W10 ~~ wstd_eu_intW10
  wstd_auth01W11 ~~ wstd_eu_intW11
  wstd_auth01W14 ~~ wstd_eu_intW14
  
  
  # Estimate the variance and covariance of the random intercepts. 
  RIstd_auth01W ~~ 0*RIstd_auth01W
  RIstd_eu_intW ~~ 0*RIstd_eu_intW
  RIstd_auth01W ~~ 0*RIstd_eu_intW

  # Estimate the (residual) variance of the within-person centered variables.
  wstd_auth01W7 ~~ wstd_auth01W7 # Variances
  wstd_eu_intW7 ~~ wstd_eu_intW7 
  wstd_auth01W10 ~~ wstd_auth01W10 # Residual variances
  wstd_eu_intW10 ~~ wstd_eu_intW10 
  wstd_auth01W11 ~~ wstd_auth01W11
  wstd_eu_intW11 ~~ wstd_eu_intW11 
  wstd_auth01W14 ~~ wstd_auth01W14 
  wstd_eu_intW14 ~~ wstd_eu_intW14 
  
  # Fix error variance of indicators to (1 - alpha)*var
std_auth01W10 ~~ 0.0058*std_auth01W10
std_auth01W11 ~~ 0.002*std_auth01W11
std_auth01W14 ~~ 0.00725*std_auth01W14
std_eu_intW10 ~~ 0.0028*std_eu_intW10
std_eu_intW11 ~~ 0.00194*std_eu_intW11
std_eu_intW14 ~~ 0*std_eu_intW14


'

std_eu_pref_CLPM_b.fit <- lavaan(std_eu_pref_CLPM_b, data = dat, 
                                 estimator = "MLR",
                                 missing = 'ML', meanstructure = T, int.ov.free = T) 


#### CLPMs - covariate models ####

# Immigration 

std_immig_pref_CLPM_tvc <- '
  # Create between components (random intercepts)
  RIauth01W  =~ 1*std_auth01W7 + 1*std_auth01W10 + 1*std_auth01W11 + 1*std_auth01W14 
  RIim_prefW =~ 1*std_im_pref_2W7 + 1*std_im_pref_2W10 + 1*std_im_pref_2W11 + 1*std_im_pref_2W14 
  
  # Create within-person centered variables
  wauth01W7 =~ 1*std_auth01W7
  wauth01W10 =~ 1*std_auth01W10
  wauth01W11 =~ 1*std_auth01W11 
  wauth01W14 =~ 1*std_auth01W14

  wim_prefW7 =~ 1*std_im_pref_2W7
  wim_prefW10 =~ 1*std_im_pref_2W10
  wim_prefW11 =~ 1*std_im_pref_2W11
  wim_prefW14 =~ 1*std_im_pref_2W14

  # Estimate the lagged effects between the within-person centered variables.
  wauth01W10 + wim_prefW10 ~ wauth01W7 + wim_prefW7
  wauth01W11 + wim_prefW11 ~ wauth01W10 + wim_prefW10
  wauth01W14 + wim_prefW14 ~ wauth01W11 + wim_prefW11

  # Estimate the covariance between the within-person centered variables at the first wave. 
  wauth01W7 ~~ wim_prefW7 # Covariance
  
  # Estimate the covariances between the residuals of the within-person centered variables (the innovations).
  wauth01W10 ~~ wim_prefW10
  wauth01W11 ~~ wim_prefW11
  wauth01W14 ~~ wim_prefW14
  
  
  # Fix the variance and covariance of the random intercepts to zero 
  RIauth01W ~~ 0*RIauth01W
  RIim_prefW ~~ 0*RIim_prefW
  RIauth01W ~~ 0*RIim_prefW

  # Estimate the (residual) variance of the within-person centered variables.
  wauth01W7 ~~ wauth01W7 # Variances
  wim_prefW7 ~~ wim_prefW7 
  wauth01W10 ~~ wauth01W10 # Residual variances
  wim_prefW10 ~~ wim_prefW10 
  wauth01W11 ~~ wauth01W11
  wim_prefW11 ~~ wim_prefW11 
  wauth01W14 ~~ wauth01W14 
  wim_prefW14 ~~ wim_prefW14 
  
# Fix error variance of indicators to (1 - alpha)*var
std_auth01W7 ~~ 0.02*std_auth01W7
std_auth01W10 ~~ 0.016*std_auth01W10
std_auth01W11 ~~ 0.018*std_auth01W11
std_auth01W14 ~~ 0.019*std_auth01W14
std_im_pref_2W7 ~~ 0.0147*std_im_pref_2W7
std_im_pref_2W10 ~~ 0.000155*std_im_pref_2W10
std_im_pref_2W11 ~~ 0.000147*std_im_pref_2W11
std_im_pref_2W14 ~~ 0.000512*std_im_pref_2W14

# incorporate tvc - age
wauth01W7 + wim_prefW7 ~ ageW7
wauth01W10 + wim_prefW10 ~ ageW10
wauth01W11 + wim_prefW11 ~ ageW11
wauth01W14 + wim_prefW14 ~ ageW14

ageW7 ~~ ageW7
ageW10 ~~ ageW10
ageW11 ~~ ageW11
ageW14 ~~ ageW14

ageW7 ~~ ageW10 + ageW11 + ageW14
ageW10 ~~ ageW11 + ageW14
ageW11 ~~ ageW14

# incorporate tvc - ethnicity
wauth01W7 + wim_prefW7 ~ ethnic7
wauth01W10 + wim_prefW10 ~ ethnic10
wauth01W11 + wim_prefW11 ~ ethnic11
wauth01W14 + wim_prefW14 ~ ethnic14

ethnic7 ~~ ethnic7
ethnic10 ~~ ethnic10
ethnic11 ~~ ethnic11
ethnic14 ~~ ethnic14

ethnic7 ~~ ethnic10 + ethnic11 + ethnic14
ethnic10 ~~ ethnic11 + ethnic14
ethnic11 ~~ ethnic14

# incorporate tvc – social grade
wauth01W7 + wim_prefW7 ~ p_socgradeW7
wauth01W10 + wim_prefW10 ~ p_socgradeW10
wauth01W11 + wim_prefW11 ~ p_socgradeW11
wauth01W14 + wim_prefW14 ~ p_socgradeW14

p_socgradeW7 ~~ p_socgradeW7
p_socgradeW10 ~~ p_socgradeW10
p_socgradeW11 ~~ p_socgradeW11
p_socgradeW14 ~~ p_socgradeW14

p_socgradeW7 ~~ p_socgradeW10 + p_socgradeW11 + p_socgradeW14
p_socgradeW10 ~~ p_socgradeW11 + p_socgradeW14
p_socgradeW11 ~~ p_socgradeW14

# incorporate tvc – education
wauth01W7 + wim_prefW7 ~ higheduc7
wauth01W10 + wim_prefW10 ~ higheduc10
wauth01W11 + wim_prefW11 ~ higheduc11
wauth01W14 + wim_prefW14 ~ higheduc14

higheduc7 ~~ higheduc7
higheduc10 ~~ higheduc10
higheduc11 ~~ higheduc11
higheduc14 ~~ higheduc14

higheduc7 ~~ higheduc10 + higheduc11 + higheduc14
higheduc10 ~~ higheduc11 + higheduc14
higheduc11 ~~ higheduc14

# incorporate tvc – income
wauth01W7 + wim_prefW7 ~ incomerec7
wauth01W10 + wim_prefW10 ~ incomerec10
wauth01W11 + wim_prefW11 ~ incomerec11
wauth01W14 + wim_prefW14 ~ incomerec14

incomerec7 ~~ incomerec7
incomerec10 ~~ incomerec10
incomerec11 ~~ incomerec11
incomerec14 ~~ incomerec14

incomerec7 ~~ incomerec10 + incomerec11 + incomerec14
incomerec10 ~~ incomerec11 + incomerec14
incomerec11 ~~ incomerec14


  '
std_immig_pref_CLPM_tvc.fit <- lavaan(std_immig_pref_CLPM_tvc,
                                  data = dat, 
                                  estimator = "MLR",
                                  missing = 'ML', 
                                  meanstructure = T, 
                                  int.ov.free = T) 
summary(std_immig_pref_CLPM_tvc.fit, standardized = T)

# Redistribution 

std_redist_pref_CLPM_tvc <- '
  # Create between components (random intercepts)
  RIstd_auth01W  =~ 1*std_auth01W7 + 1*std_auth01W10 + 1*std_auth01W11 + 1*std_auth01W14 
  RIstd_redist_prefW =~ 1*std_redistSelf01W7 + 1*std_redistSelf01W10 + 1*std_redistSelf01W11 + 1*std_redistSelf01W14 
  
  # Create within-person centered variables
  wstd_auth01W7 =~ 1*std_auth01W7
  wstd_auth01W10 =~ 1*std_auth01W10
  wstd_auth01W11 =~ 1*std_auth01W11 
  wstd_auth01W14 =~ 1*std_auth01W14

  wstd_redist_prefW7 =~ 1*std_redistSelf01W7
  wstd_redist_prefW10 =~ 1*std_redistSelf01W10
  wstd_redist_prefW11 =~ 1*std_redistSelf01W11
  wstd_redist_prefW14 =~ 1*std_redistSelf01W14

  # Estimate the lagged effects between the within-person centered variables.
  wstd_auth01W10 + wstd_redist_prefW10 ~ wstd_auth01W7 + wstd_redist_prefW7
  wstd_auth01W11 + wstd_redist_prefW11 ~ wstd_auth01W10 + wstd_redist_prefW10
  wstd_auth01W14 + wstd_redist_prefW14 ~ wstd_auth01W11 + wstd_redist_prefW11

  # Estimate the covariance between the within-person centered variables at the first wave. 
  wstd_auth01W7 ~~ wstd_redist_prefW7 # Covariance
  
  # Estimate the covariances between the residuals of the within-person centered variables (the innovations).
  wstd_auth01W10 ~~ wstd_redist_prefW10
  wstd_auth01W11 ~~ wstd_redist_prefW11
  wstd_auth01W14 ~~ wstd_redist_prefW14
  
  
  # Constrain the variance and covariance of the random intercepts to zero  
  RIstd_auth01W ~~ 0*RIstd_auth01W
  RIstd_redist_prefW ~~ 0*RIstd_redist_prefW
  RIstd_auth01W ~~ 0*RIstd_redist_prefW

  # Estimate the (residual) variance of the within-person centered variables.
  wstd_auth01W7 ~~ wstd_auth01W7 # Variances
  wstd_redist_prefW7 ~~ wstd_redist_prefW7 
  wstd_auth01W10 ~~ wstd_auth01W10 # Residual variances
  wstd_redist_prefW10 ~~ wstd_redist_prefW10 
  wstd_auth01W11 ~~ wstd_auth01W11
  wstd_redist_prefW11 ~~ wstd_redist_prefW11 
  wstd_auth01W14 ~~ wstd_auth01W14 
  wstd_redist_prefW14 ~~ wstd_redist_prefW14 
  
  # Fix error variance of indicators to (1 - alpha)*var
std_auth01W10 ~~ 0.0058*std_auth01W10
std_auth01W11 ~~ 0.002*std_auth01W11
std_auth01W14 ~~ 0.00725*std_auth01W14
std_redistSelf01W10 ~~ 0.01038*std_redistSelf01W10
std_redistSelf01W11 ~~ 0.00933*std_redistSelf01W11
std_redistSelf01W14 ~~ 0*std_redistSelf01W14

# incorporate tvc - age
wstd_auth01W7 + wstd_redist_prefW7 ~ ageW7
wstd_auth01W10 + wstd_redist_prefW10 ~ ageW10
wstd_auth01W11 + wstd_redist_prefW11 ~ ageW11
wstd_auth01W14 + wstd_redist_prefW14 ~ ageW14

ageW7 ~~ ageW7
ageW10 ~~ ageW10
ageW11 ~~ ageW11
ageW14 ~~ ageW14

ageW7 ~~ ageW10 + ageW11 + ageW14
ageW10 ~~ ageW11 + ageW14
ageW11 ~~ ageW14

# incorporate tvc - ethnicity
wstd_auth01W7 + wstd_redist_prefW7 ~ ethnic7
wstd_auth01W10 + wstd_redist_prefW10 ~ ethnic10
wstd_auth01W11 + wstd_redist_prefW11 ~ ethnic11
wstd_auth01W14 + wstd_redist_prefW14 ~ ethnic14

ethnic7 ~~ ethnic7
ethnic10 ~~ ethnic10
ethnic11 ~~ ethnic11
ethnic14 ~~ ethnic14

ethnic7 ~~ ethnic10 + ethnic11 + ethnic14
ethnic10 ~~ ethnic11 + ethnic14
ethnic11 ~~ ethnic14

# incorporate tvc – social grade
wstd_auth01W7 + wstd_redist_prefW7 ~ p_socgradeW7
wstd_auth01W10 + wstd_redist_prefW10 ~ p_socgradeW10
wstd_auth01W11 + wstd_redist_prefW11 ~ p_socgradeW11
wstd_auth01W14 + wstd_redist_prefW14 ~ p_socgradeW14

p_socgradeW7 ~~ p_socgradeW7
p_socgradeW10 ~~ p_socgradeW10
p_socgradeW11 ~~ p_socgradeW11
p_socgradeW14 ~~ p_socgradeW14

p_socgradeW7 ~~ p_socgradeW10 + p_socgradeW11 + p_socgradeW14
p_socgradeW10 ~~ p_socgradeW11 + p_socgradeW14
p_socgradeW11 ~~ p_socgradeW14

# incorporate tvc – education
wstd_auth01W7 + wstd_redist_prefW7 ~ higheduc7
wstd_auth01W10 + wstd_redist_prefW10 ~ higheduc10
wstd_auth01W11 + wstd_redist_prefW11 ~ higheduc11
wstd_auth01W14 + wstd_redist_prefW14 ~ higheduc14

higheduc7 ~~ higheduc7
higheduc10 ~~ higheduc10
higheduc11 ~~ higheduc11
higheduc14 ~~ higheduc14

higheduc7 ~~ higheduc10 + higheduc11 + higheduc14
higheduc10 ~~ higheduc11 + higheduc14
higheduc11 ~~ higheduc14

# incorporate tvc – income
wstd_auth01W7 + wstd_redist_prefW7 ~ incomerec7
wstd_auth01W10 + wstd_redist_prefW10 ~ incomerec10
wstd_auth01W11 + wstd_redist_prefW11 ~ incomerec11
wstd_auth01W14 + wstd_redist_prefW14 ~ incomerec14

incomerec7 ~~ incomerec7
incomerec10 ~~ incomerec10
incomerec11 ~~ incomerec11
incomerec14 ~~ incomerec14

incomerec7 ~~ incomerec10 + incomerec11 + incomerec14
incomerec10 ~~ incomerec11 + incomerec14
incomerec11 ~~ incomerec14


'

std_redist_pref_CLPM_tvc.fit <- lavaan(std_redist_pref_CLPM_tvc, data = dat, 
                                       estimator = "MLR",
                                       missing = 'ML', meanstructure = T, int.ov.free = T) 
summary(std_redist_pref_CLPM_tvc.fit, standardized = T)

# EU integration 

std_eu_pref_CLPM_tvc <- '
  # Create between components (random intercepts)
  RIstd_auth01W  =~ 1*std_auth01W7 + 1*std_auth01W10 + 1*std_auth01W11 + 1*std_auth01W14 
  RIstd_eu_intW =~ 1*std_eu_intW7 + 1*std_eu_intW10 + 1*std_eu_intW11 + 1*std_eu_intW14 
  
  # Create within-person centered variables
  wstd_auth01W7 =~ 1*std_auth01W7
  wstd_auth01W10 =~ 1*std_auth01W10
  wstd_auth01W11 =~ 1*std_auth01W11 
  wstd_auth01W14 =~ 1*std_auth01W14

  wstd_eu_intW7 =~ 1*std_eu_intW7
  wstd_eu_intW10 =~ 1*std_eu_intW10
  wstd_eu_intW11 =~ 1*std_eu_intW11
  wstd_eu_intW14 =~ 1*std_eu_intW14

  # Estimate the lagged effects between the within-person centered variables.
  wstd_auth01W10 + wstd_eu_intW10 ~ wstd_auth01W7 + wstd_eu_intW7
  wstd_auth01W11 + wstd_eu_intW11 ~ wstd_auth01W10 + wstd_eu_intW10
  wstd_auth01W14 + wstd_eu_intW14 ~ wstd_auth01W11 + wstd_eu_intW11

  # Estimate the covariance between the within-person centered variables at the first wave. 
  wstd_auth01W7 ~~ wstd_eu_intW7 # Covariance
  
  # Estimate the covariances between the residuals of the within-person centered variables (the innovations).
  wstd_auth01W10 ~~ wstd_eu_intW10
  wstd_auth01W11 ~~ wstd_eu_intW11
  wstd_auth01W14 ~~ wstd_eu_intW14
  
  
  # Estimate the variance and covariance of the random intercepts. 
  RIstd_auth01W ~~ 0*RIstd_auth01W
  RIstd_eu_intW ~~ 0*RIstd_eu_intW
  RIstd_auth01W ~~ 0*RIstd_eu_intW

  # Estimate the (residual) variance of the within-person centered variables.
  wstd_auth01W7 ~~ wstd_auth01W7 # Variances
  wstd_eu_intW7 ~~ wstd_eu_intW7 
  wstd_auth01W10 ~~ wstd_auth01W10 # Residual variances
  wstd_eu_intW10 ~~ wstd_eu_intW10 
  wstd_auth01W11 ~~ wstd_auth01W11
  wstd_eu_intW11 ~~ wstd_eu_intW11 
  wstd_auth01W14 ~~ wstd_auth01W14 
  wstd_eu_intW14 ~~ wstd_eu_intW14 
  
  # Fix error variance of indicators to (1 - alpha)*var
std_auth01W10 ~~ 0.0058*std_auth01W10
std_auth01W11 ~~ 0.002*std_auth01W11
std_auth01W14 ~~ 0.00725*std_auth01W14
std_eu_intW10 ~~ 0.0028*std_eu_intW10
std_eu_intW11 ~~ 0.00194*std_eu_intW11
std_eu_intW14 ~~ 0*std_eu_intW14

# incorporate tvc - age
wstd_auth01W7 + wstd_eu_intW7 ~ ageW7
wstd_auth01W10 + wstd_eu_intW10 ~ ageW10
wstd_auth01W11 + wstd_eu_intW11 ~ ageW11
wstd_auth01W14 + wstd_eu_intW14 ~ ageW14

ageW7 ~~ ageW7
ageW10 ~~ ageW10
ageW11 ~~ ageW11
ageW14 ~~ ageW14

ageW7 ~~ ageW10 + ageW11 + ageW14
ageW10 ~~ ageW11 + ageW14
ageW11 ~~ ageW14

# incorporate tvc - ethnicity
wstd_auth01W7 + wstd_eu_intW7 ~ ethnic7
wstd_auth01W10 + wstd_eu_intW10 ~ ethnic10
wstd_auth01W11 + wstd_eu_intW11 ~ ethnic11
wstd_auth01W14 + wstd_eu_intW14 ~ ethnic14

ethnic7 ~~ ethnic7
ethnic10 ~~ ethnic10
ethnic11 ~~ ethnic11
ethnic14 ~~ ethnic14

ethnic7 ~~ ethnic10 + ethnic11 + ethnic14
ethnic10 ~~ ethnic11 + ethnic14
ethnic11 ~~ ethnic14

# incorporate tvc – social grade
wstd_auth01W7 + wstd_eu_intW7 ~ p_socgradeW7
wstd_auth01W10 + wstd_eu_intW10 ~ p_socgradeW10
wstd_auth01W11 + wstd_eu_intW11 ~ p_socgradeW11
wstd_auth01W14 + wstd_eu_intW14 ~ p_socgradeW14

p_socgradeW7 ~~ p_socgradeW7
p_socgradeW10 ~~ p_socgradeW10
p_socgradeW11 ~~ p_socgradeW11
p_socgradeW14 ~~ p_socgradeW14

p_socgradeW7 ~~ p_socgradeW10 + p_socgradeW11 + p_socgradeW14
p_socgradeW10 ~~ p_socgradeW11 + p_socgradeW14
p_socgradeW11 ~~ p_socgradeW14

# incorporate tvc – education
wstd_auth01W7 + wstd_eu_intW7 ~ higheduc7
wstd_auth01W10 + wstd_eu_intW10 ~ higheduc10
wstd_auth01W11 + wstd_eu_intW11 ~ higheduc11
wstd_auth01W14 + wstd_eu_intW14 ~ higheduc14

higheduc7 ~~ higheduc7
higheduc10 ~~ higheduc10
higheduc11 ~~ higheduc11
higheduc14 ~~ higheduc14

higheduc7 ~~ higheduc10 + higheduc11 + higheduc14
higheduc10 ~~ higheduc11 + higheduc14
higheduc11 ~~ higheduc14

# incorporate tvc – income
wstd_auth01W7 + wstd_eu_intW7 ~ incomerec7
wstd_auth01W10 + wstd_eu_intW10 ~ incomerec10
wstd_auth01W11 + wstd_eu_intW11 ~ incomerec11
wstd_auth01W14 + wstd_eu_intW14 ~ incomerec14

incomerec7 ~~ incomerec7
incomerec10 ~~ incomerec10
incomerec11 ~~ incomerec11
incomerec14 ~~ incomerec14

incomerec7 ~~ incomerec10 + incomerec11 + incomerec14
incomerec10 ~~ incomerec11 + incomerec14
incomerec11 ~~ incomerec14

'

std_eu_pref_CLPM_tvc.fit <- lavaan(std_eu_pref_CLPM_tvc, data = dat, 
                                   estimator = "MLR",
                                   missing = 'ML', meanstructure = T, int.ov.free = T) 
summary(std_eu_pref_CLPM_tvc.fit, standardized = T)

#### Equality constraints - RI-CLPMs ####

## Immigration:

# Baseline

immig_pref_RICLPM_EQ <- '
  # Create between components (random intercepts)
  RIauth01W  =~ 1*std_auth01W7 + 1*std_auth01W10 + 1*std_auth01W11 + 1*std_auth01W14 
  RIim_prefW =~ 1*std_im_pref_2W7 + 1*std_im_pref_2W10 + 1*std_im_pref_2W11 + 1*std_im_pref_2W14 
  
  # Create within-person centered variables
  wauth01W7 =~ NA*std_auth01W7
  wauth01W10 =~ NA*std_auth01W10
  wauth01W11 =~ NA*std_auth01W11 
  wauth01W14 =~ NA*std_auth01W14

  wim_prefW7 =~ NA*std_im_pref_2W7
  wim_prefW10 =~ NA*std_im_pref_2W10
  wim_prefW11 =~ NA*std_im_pref_2W11
  wim_prefW14 =~ NA*std_im_pref_2W14

  # Estimate the lagged effects between the within-person centered variables.
  wauth01W10 ~ a*wauth01W7 + b*wim_prefW7
  wim_prefW10 ~ c*wauth01W7 + d*wim_prefW7
  wauth01W11 ~ a*wauth01W10 + b*wim_prefW10
  wim_prefW11 ~ c*wauth01W10 + d*wim_prefW10
  wauth01W14 ~ a*wauth01W11 + b*wim_prefW11
  wim_prefW14 ~ c*wauth01W11 + d*wim_prefW11

  # Estimate the covariance between the within-person centered variables at the first wave. 
  wauth01W7 ~~ cor1*wim_prefW7 # Covariance
  
  # Label residual covariances
  wauth01W10 ~~ rcov2*wim_prefW10
  wauth01W11 ~~ rcov3*wim_prefW11
  wauth01W14 ~~ rcov4*wim_prefW14
  
  
  # Estimate the variance and covariance of the random intercepts 
  RIauth01W ~~ RIauth01W
  RIim_prefW ~~ RIim_prefW
  RIauth01W ~~ RIim_prefW

  # Set variances of within-components at first wave to 1
  wauth01W7 ~~ 1*wauth01W7 # Variances
  wim_prefW7 ~~ 1*wim_prefW7
  
  wauth01W10 ~~ rvx2*wauth01W10 # Residual variances
  wim_prefW10 ~~ rvy2*wim_prefW10 
  wauth01W11 ~~ rvx3*wauth01W11
  wim_prefW11 ~~ rvy3*wim_prefW11 
  wauth01W14 ~~ rvx4*wauth01W14 
  wim_prefW14 ~~ rvy4*wim_prefW14 
  
# Fix error variance of indicators to (1 - alpha)*var
std_auth01W7 ~~ 0.02*std_auth01W7
std_auth01W10 ~~ 0.016*std_auth01W10
std_auth01W11 ~~ 0.018*std_auth01W11
std_auth01W14 ~~ 0.019*std_auth01W14
std_im_pref_2W7 ~~ 0.0147*std_im_pref_2W7
std_im_pref_2W10 ~~ 0.000155*std_im_pref_2W10
std_im_pref_2W11 ~~ 0.000147*std_im_pref_2W11
std_im_pref_2W14 ~~ 0.000512*std_im_pref_2W14


 # Compute correlations of within-components at each wave
  cor2 := a*c + b*d + a*d*cor1 + b*c*cor1 + rcov2
  cor3 := a*c + b*d + a*d*cor2 + b*c*cor2 + rcov3
  cor4 := a*c + b*d + a*d*cor3 + b*c*cor3 + rcov4
 
  # Contrain residual variances of within-components such that variance of each 
  # within-component equals 1
  rvx2 == 1 - (a*a + b*b + 2*a*b*cor1)
  rvy2 == 1 - (c*c + d*d + 2*c*d*cor1)
  rvx3 == 1 - (a*a + b*b + 2*a*b*cor2)
  rvy3 == 1 - (c*c + d*d + 2*c*d*cor2)
  rvx4 == 1 - (a*a + b*b + 2*a*b*cor3)
  rvy4 == 1 - (c*c + d*d + 2*c*d*cor3)

  '
immig_pref_RICLPM_EQ.fit <- lavaan(immig_pref_RICLPM_EQ,
                                   data = dat, 
                                   estimator = "MLR",
                                   missing = 'ML', 
                                   meanstructure = T, 
                                   int.ov.free = T) 
summary(immig_pref_RICLPM_EQ.fit, standardized = T)

# Covariate 

immig_pref_2_tvc_EQ <- '
  # Create between components (random intercepts)
  RIauth01W  =~ 1*std_auth01W7 + 1*std_auth01W10 + 1*std_auth01W11 + 1*std_auth01W14 
  RIim_prefW =~ 1*std_im_pref_2W7 + 1*std_im_pref_2W10 + 1*std_im_pref_2W11 + 1*std_im_pref_2W14 
  
  # Create within-person centered variables
  wauth01W7 =~ NA*std_auth01W7
  wauth01W10 =~ NA*std_auth01W10
  wauth01W11 =~ NA*std_auth01W11 
  wauth01W14 =~ NA*std_auth01W14

  wim_prefW7 =~ NA*std_im_pref_2W7
  wim_prefW10 =~ NA*std_im_pref_2W10
  wim_prefW11 =~ NA*std_im_pref_2W11
  wim_prefW14 =~ NA*std_im_pref_2W14

  # Estimate the lagged effects between the within-person centered variables.
  wauth01W10 ~ a*wauth01W7 + b*wim_prefW7
  wim_prefW10 ~ c*wauth01W7 + d*wim_prefW7
  wauth01W11 ~ a*wauth01W10 + b*wim_prefW10
  wim_prefW11 ~ c*wauth01W10 + d*wim_prefW10
  wauth01W14 ~ a*wauth01W11 + b*wim_prefW11
  wim_prefW14 ~ c*wauth01W11 + d*wim_prefW11

  # Estimate the covariance between the within-person centered variables at the first wave. 
  wauth01W7 ~~ cor1*wim_prefW7 # Covariance
  
  # Estimate the covariances between the residuals of the within-person centered variables (the innovations).
  wauth01W10 ~~ rcov2*wim_prefW10
  wauth01W11 ~~ rcov3*wim_prefW11
  wauth01W14 ~~ rcov4*wim_prefW14
 
  # Estimate the variance and covariance of the random intercepts. 
  RIauth01W ~~ 0.691*RIauth01W
  RIim_prefW ~~ 0.746*RIim_prefW
  RIauth01W ~~ RIim_prefW

  # Estimate the (residual) variance of the within-person centered variables.
  wauth01W7 ~~ 1*wauth01W7 # Variances
  wim_prefW7 ~~ 1*wim_prefW7 
  
  # Label residual variances
  wauth01W10 ~~ rvx2*wauth01W10 # Residual variances
  wim_prefW10 ~~ rvy2*wim_prefW10 
  wauth01W11 ~~ rvx3*wauth01W11
  wim_prefW11 ~~ rvy3*wim_prefW11 
  wauth01W14 ~~ rvx4*wauth01W14 
  wim_prefW14 ~~ rvy4*wim_prefW14 
  
# Fix error variance of indicators to (1 - alpha)*var
std_auth01W7 ~~ 0.02*std_auth01W7
std_auth01W10 ~~ 0.016*std_auth01W10
std_auth01W11 ~~ 0.018*std_auth01W11
std_auth01W14 ~~ 0.019*std_auth01W14
std_im_pref_2W7 ~~ 0.0147*std_im_pref_2W7
std_im_pref_2W10 ~~ 0.000155*std_im_pref_2W10
std_im_pref_2W11 ~~ 0.000147*std_im_pref_2W11
std_im_pref_2W14 ~~ 0.000512*std_im_pref_2W14

# incorporate tvc - age
wauth01W7 + wim_prefW7 ~ ageW7
wauth01W10 + wim_prefW10 ~ ageW10
wauth01W11 + wim_prefW11 ~ ageW11
wauth01W14 + wim_prefW14 ~ ageW14

ageW7 ~~ ageW7
ageW10 ~~ ageW10
ageW11 ~~ ageW11
ageW14 ~~ ageW14

ageW7 ~~ ageW10 + ageW11 + ageW14
ageW10 ~~ ageW11 + ageW14
ageW11 ~~ ageW14

# incorporate tvc – social grade
wauth01W7 + wim_prefW7 ~ p_socgradeW7
wauth01W10 + wim_prefW10 ~ p_socgradeW10
wauth01W11 + wim_prefW11 ~ p_socgradeW11
wauth01W14 + wim_prefW14 ~ p_socgradeW14

p_socgradeW7 ~~ p_socgradeW7
p_socgradeW10 ~~ p_socgradeW10
p_socgradeW11 ~~ p_socgradeW11
p_socgradeW14 ~~ p_socgradeW14

p_socgradeW7 ~~ p_socgradeW10 + p_socgradeW11 + p_socgradeW14
p_socgradeW10 ~~ p_socgradeW11 + p_socgradeW14
p_socgradeW11 ~~ p_socgradeW14

# incorporate tvc – income
wauth01W7 + wim_prefW7 ~ incomerec7
wauth01W10 + wim_prefW10 ~ incomerec10
wauth01W11 + wim_prefW11 ~ incomerec11
wauth01W14 + wim_prefW14 ~ incomerec14

incomerec7 ~~ incomerec7
incomerec10 ~~ incomerec10
incomerec11 ~~ incomerec11
incomerec14 ~~ incomerec14

incomerec7 ~~ incomerec10 + incomerec11 + incomerec14
incomerec10 ~~ incomerec11 + incomerec14
incomerec11 ~~ incomerec14

 # Compute correlations of within-components at each wave
  cor2 := a*c + b*d + a*d*cor1 + b*c*cor1 + rcov2
  cor3 := a*c + b*d + a*d*cor2 + b*c*cor2 + rcov3
  cor4 := a*c + b*d + a*d*cor3 + b*c*cor3 + rcov4
 
  # Contrain residual variances of within-components such that variance of each 
  # within-component equals 1
  rvx2 == 1 - (a*a + b*b + 2*a*b*cor1)
  rvy2 == 1 - (c*c + d*d + 2*c*d*cor1)
  rvx3 == 1 - (a*a + b*b + 2*a*b*cor2)
  rvy3 == 1 - (c*c + d*d + 2*c*d*cor2)
  rvx4 == 1 - (a*a + b*b + 2*a*b*cor3)
  rvy4 == 1 - (c*c + d*d + 2*c*d*cor3)
  '
immig_pref_2_tvc_EQ.fit <- lavaan(immig_pref_2_tvc_EQ, 
                                     data = dat, 
                                     estimator = "MLR",
                                     missing = 'ML', 
                                     meanstructure = T, 
                                     int.ov.free = T) 
summary(immig_pref_2_tvc_EQ.fit, standardized = T)

## Redistribution

# Baseline 

redist_RICLPM_b_EQ <- '
  # Create between components (random intercepts)
  RIstd_auth01W  =~ 1*std_auth01W7 + 1*std_auth01W10 + 1*std_auth01W11 + 1*std_auth01W14 
  RIstd_redist_prefW =~ 1*std_redistSelf01W7 + 1*std_redistSelf01W10 + 1*std_redistSelf01W11 + 1*std_redistSelf01W14 
  
  # Create within-person centered variables
  wstd_auth01W7 =~ NA*std_auth01W7
  wstd_auth01W10 =~ NA*std_auth01W10
  wstd_auth01W11 =~ NA*std_auth01W11 
  wstd_auth01W14 =~ NA*std_auth01W14

  wstd_redist_prefW7 =~ NA*std_redistSelf01W7
  wstd_redist_prefW10 =~ NA*std_redistSelf01W10
  wstd_redist_prefW11 =~ NA*std_redistSelf01W11
  wstd_redist_prefW14 =~ NA*std_redistSelf01W14

  # Estimate the lagged effects between the within-person centered variables.
  wstd_auth01W10 ~ a*wstd_auth01W7 + b*wstd_redist_prefW7
  wstd_redist_prefW10 ~ c*wstd_auth01W7 + d*wstd_redist_prefW7
  wstd_auth01W11 ~ a*wstd_auth01W10 + b*wstd_redist_prefW10
  wstd_redist_prefW11 ~ c*wstd_auth01W10 + d*wstd_redist_prefW10
  wstd_auth01W14 ~ a*wstd_auth01W11 + b*wstd_redist_prefW11
  wstd_redist_prefW14 ~ c*wstd_auth01W11 + d*wstd_redist_prefW11

  # Estimate the covariance between the within-person centered variables at the first wave. 
  wstd_auth01W7 ~~ cor1*wstd_redist_prefW7 # Covariance
  
  # Estimate the covariances between the residuals of the within-person centered variables (the innovations).
  wstd_auth01W10 ~~ rcov2*wstd_redist_prefW10
  wstd_auth01W11 ~~ rcov3*wstd_redist_prefW11
  wstd_auth01W14 ~~ rcov4*wstd_redist_prefW14
  
  
  # Estimate the variance and covariance of the random intercepts. 
  RIstd_auth01W ~~ RIstd_auth01W
  RIstd_redist_prefW ~~ RIstd_redist_prefW
  RIstd_auth01W ~~ RIstd_redist_prefW

  # Set variances of within-components at first wave to 1
  wstd_auth01W7 ~~ 1*wstd_auth01W7 # Variances
  wstd_redist_prefW7 ~~ 1*wstd_redist_prefW7 
  
  # Label residual variances
  wstd_auth01W10 ~~ rvx2*wstd_auth01W10 # Residual variances
  wstd_redist_prefW10 ~~ rvy2*wstd_redist_prefW10 
  wstd_auth01W11 ~~ rvx3*wstd_auth01W11
  wstd_redist_prefW11 ~~ rvy3*wstd_redist_prefW11 
  wstd_auth01W14 ~~ rvx4*wstd_auth01W14 
  wstd_redist_prefW14 ~~ rvy4*wstd_redist_prefW14 
  
  # Fix error variance of indicators to (1 - alpha)*var
std_auth01W10 ~~ 0.0058*std_auth01W10
std_auth01W11 ~~ 0.002*std_auth01W11
std_auth01W14 ~~ 0.00725*std_auth01W14
std_redistSelf01W10 ~~ 0.01038*std_redistSelf01W10
std_redistSelf01W11 ~~ 0.00933*std_redistSelf01W11
std_redistSelf01W14 ~~ 0*std_redistSelf01W14

 # Compute correlations of within-components at each wave
  cor2 := a*c + b*d + a*d*cor1 + b*c*cor1 + rcov2
  cor3 := a*c + b*d + a*d*cor2 + b*c*cor2 + rcov3
  cor4 := a*c + b*d + a*d*cor3 + b*c*cor3 + rcov4
 
  # Contrain residual variances of within-components such that variance of each 
  # within-component equals 1
  rvx2 == 1 - (a*a + b*b + 2*a*b*cor1)
  rvy2 == 1 - (c*c + d*d + 2*c*d*cor1)
  rvx3 == 1 - (a*a + b*b + 2*a*b*cor2)
  rvy3 == 1 - (c*c + d*d + 2*c*d*cor2)
  rvx4 == 1 - (a*a + b*b + 2*a*b*cor3)
  rvy4 == 1 - (c*c + d*d + 2*c*d*cor3)

'

redist_RICLPM_b_EQ.fit <- lavaan(redist_RICLPM_b_EQ, 
                                 data = dat, 
                                 estimator = "MLR",
                                 missing = 'ML',
                                 meanstructure = T, int.ov.free = T) 
summary(redist_RICLPM_b_EQ.fit, standardized = T)

# Covariate

redist_RICLPM_tvc_EQ <- '
  # Create between components (random intercepts)
  RIstd_auth01W  =~ 1*std_auth01W7 + 1*std_auth01W10 + 1*std_auth01W11 + 1*std_auth01W14 
  RIstd_redist_prefW =~ 1*std_redistSelf01W7 + 1*std_redistSelf01W10 + 1*std_redistSelf01W11 + 1*std_redistSelf01W14 
  
  # Create within-person centered variables
  wstd_auth01W7 =~ NA*std_auth01W7
  wstd_auth01W10 =~ NA*std_auth01W10
  wstd_auth01W11 =~ NA*std_auth01W11 
  wstd_auth01W14 =~ NA*std_auth01W14

  wstd_redist_prefW7 =~ NA*std_redistSelf01W7
  wstd_redist_prefW10 =~ NA*std_redistSelf01W10
  wstd_redist_prefW11 =~ NA*std_redistSelf01W11
  wstd_redist_prefW14 =~ NA*std_redistSelf01W14

  # Estimate the lagged effects between the within-person centered variables.
  wstd_auth01W10 ~ a*wstd_auth01W7 + b*wstd_redist_prefW7
  wstd_redist_prefW10 ~ c*wstd_auth01W7 + d*wstd_redist_prefW7
  wstd_auth01W11 ~ a*wstd_auth01W10 + b*wstd_redist_prefW10
  wstd_redist_prefW11 ~ c*wstd_auth01W10 + d*wstd_redist_prefW10
  wstd_auth01W14 ~ a*wstd_auth01W11 + b*wstd_redist_prefW11
  wstd_redist_prefW14 ~ c*wstd_auth01W11 + d*wstd_redist_prefW11

  # Estimate the covariance between the within-person centered variables at the first wave. 
  wstd_auth01W7 ~~ cor1*wstd_redist_prefW7 # Covariance
  
  # Estimate the covariances between the residuals of the within-person centered variables (the innovations).
  wstd_auth01W10 ~~ rcov2*wstd_redist_prefW10
  wstd_auth01W11 ~~ rcov3*wstd_redist_prefW11
  wstd_auth01W14 ~~ rcov4*wstd_redist_prefW14
  
  
  # Estimate the variance and covariance of the random intercepts. 
  RIstd_auth01W ~~ RIstd_auth01W
  RIstd_redist_prefW ~~ RIstd_redist_prefW
  RIstd_auth01W ~~ RIstd_redist_prefW

  # Set variances of within-components at first wave to 1
  wstd_auth01W7 ~~ 1*wstd_auth01W7 # Variances
  wstd_redist_prefW7 ~~ 1*wstd_redist_prefW7 
  
  # Label residual variances
  wstd_auth01W10 ~~ rvx2*wstd_auth01W10 # Residual variances
  wstd_redist_prefW10 ~~ rvy2*wstd_redist_prefW10 
  wstd_auth01W11 ~~ rvx3*wstd_auth01W11
  wstd_redist_prefW11 ~~ rvy3*wstd_redist_prefW11 
  wstd_auth01W14 ~~ rvx4*wstd_auth01W14 
  wstd_redist_prefW14 ~~ rvy4*wstd_redist_prefW14 
  
  # Fix error variance of indicators to (1 - alpha)*var
std_auth01W10 ~~ 0.0058*std_auth01W10
std_auth01W11 ~~ 0.002*std_auth01W11
std_auth01W14 ~~ 0.00725*std_auth01W14
std_redistSelf01W10 ~~ 0.01038*std_redistSelf01W10
std_redistSelf01W11 ~~ 0.00933*std_redistSelf01W11
std_redistSelf01W14 ~~ 0*std_redistSelf01W14

 # Compute correlations of within-components at each wave
  cor2 := a*c + b*d + a*d*cor1 + b*c*cor1 + rcov2
  cor3 := a*c + b*d + a*d*cor2 + b*c*cor2 + rcov3
  cor4 := a*c + b*d + a*d*cor3 + b*c*cor3 + rcov4
 
  # Contrain residual variances of within-components such that variance of each 
  # within-component equals 1
  rvx2 == 1 - (a*a + b*b + 2*a*b*cor1)
  rvy2 == 1 - (c*c + d*d + 2*c*d*cor1)
  rvx3 == 1 - (a*a + b*b + 2*a*b*cor2)
  rvy3 == 1 - (c*c + d*d + 2*c*d*cor2)
  rvx4 == 1 - (a*a + b*b + 2*a*b*cor3)
  rvy4 == 1 - (c*c + d*d + 2*c*d*cor3)
  
  # incorporate tvc - age
wstd_auth01W7 + wstd_redist_prefW7 ~ ageW7
wstd_auth01W10 + wstd_redist_prefW10 ~ ageW10
wstd_auth01W11 + wstd_redist_prefW11 ~ ageW11
wstd_auth01W14 + wstd_redist_prefW14 ~ ageW14

ageW7 ~~ ageW7
ageW10 ~~ ageW10
ageW11 ~~ ageW11
ageW14 ~~ ageW14

ageW7 ~~ ageW10 + ageW11 + ageW14
ageW10 ~~ ageW11 + ageW14
ageW11 ~~ ageW14

# incorporate tvc – income
wstd_auth01W7 + wstd_redist_prefW7 ~ incomerec7
wstd_auth01W10 + wstd_redist_prefW10 ~ incomerec10
wstd_auth01W11 + wstd_redist_prefW11 ~ incomerec11
wstd_auth01W14 + wstd_redist_prefW14 ~ incomerec14

incomerec7 ~~ incomerec7
incomerec10 ~~ incomerec10
incomerec11 ~~ incomerec11
incomerec14 ~~ incomerec14

incomerec7 ~~ incomerec10 + incomerec11 + incomerec14
incomerec10 ~~ incomerec11 + incomerec14
incomerec11 ~~ incomerec14

# incorporate tvc – social grade
wstd_auth01W7 + wstd_redist_prefW7 ~ p_socgradeW7
wstd_auth01W10 + wstd_redist_prefW10 ~ p_socgradeW10
wstd_auth01W11 + wstd_redist_prefW11 ~ p_socgradeW11
wstd_auth01W14 + wstd_redist_prefW14 ~ p_socgradeW14

p_socgradeW7 ~~ p_socgradeW7
p_socgradeW10 ~~ p_socgradeW10
p_socgradeW11 ~~ p_socgradeW11
p_socgradeW14 ~~ p_socgradeW14

p_socgradeW7 ~~ p_socgradeW10 + p_socgradeW11 + p_socgradeW14
p_socgradeW10 ~~ p_socgradeW11 + p_socgradeW14
p_socgradeW11 ~~ p_socgradeW14

'

redist_RICLPM_tvc_EQ.fit <- lavaan(redist_RICLPM_tvc_EQ, 
                                       data = dat, 
                                       estimator = "MLR",
                                       missing = 'ML',
                                       meanstructure = T, int.ov.free = T) 
summary(redist_RICLPM_tvc_EQ.fit, standardized = T)

## EU integration

eu_RICLPM_b_EQ <- '
  # Create between components (random intercepts)
  RIstd_auth01W  =~ 1*std_auth01W7 + 1*std_auth01W10 + 1*std_auth01W11 + 1*std_auth01W14 
  RIstd_eu_intW =~ 1*std_eu_intW7 + 1*std_eu_intW10 + 1*std_eu_intW11 + 1*std_eu_intW14 
  
  # Create within-person centered variables
  wstd_auth01W7 =~ NA*std_auth01W7
  wstd_auth01W10 =~ NA*std_auth01W10
  wstd_auth01W11 =~ NA*std_auth01W11 
  wstd_auth01W14 =~ NA*std_auth01W14

  wstd_eu_intW7 =~ NA*std_eu_intW7
  wstd_eu_intW10 =~ NA*std_eu_intW10
  wstd_eu_intW11 =~ NA*std_eu_intW11
  wstd_eu_intW14 =~ NA*std_eu_intW14

  # Estimate the lagged effects between the within-person centered variables.
  wstd_auth01W10 ~ a*wstd_auth01W7 + b*wstd_eu_intW7
  wstd_eu_intW10 ~ c*wstd_auth01W7 + d*wstd_eu_intW7
  wstd_auth01W11 ~ a*wstd_auth01W10 + b*wstd_eu_intW10
  wstd_eu_intW11 ~ c*wstd_auth01W10 + d*wstd_eu_intW10
  wstd_auth01W14 ~ a*wstd_auth01W11 + b*wstd_eu_intW11
  wstd_eu_intW14 ~ c*wstd_auth01W11 + d*wstd_eu_intW11

  # Estimate the covariance between the within-person centered variables at the first wave. 
  wstd_auth01W7 ~~ cor1*wstd_eu_intW7 # Covariance
  
  # Label residual covariances
  wstd_auth01W10 ~~ rcov2*wstd_eu_intW10
  wstd_auth01W11 ~~ rcov3*wstd_eu_intW11
  wstd_auth01W14 ~~ rcov4*wstd_eu_intW14
  
  
  # Estimate the variance and covariance of the random intercepts. 
  RIstd_auth01W ~~ RIstd_auth01W
  RIstd_eu_intW ~~ RIstd_eu_intW
  RIstd_auth01W ~~ RIstd_eu_intW
  
  # Set variances of within-components at first wave to 1
  wstd_auth01W7 ~~ 1*wstd_auth01W7 # Variances
  wstd_eu_intW7 ~~ 1*wstd_eu_intW7 
  
  # Label residual variances
  wstd_auth01W10 ~~ rvx2*wstd_auth01W10 # Residual variances
  wstd_eu_intW10 ~~ rvy2*wstd_eu_intW10 
  wstd_auth01W11 ~~ rvx3*wstd_auth01W11
  wstd_eu_intW11 ~~ rvy3*wstd_eu_intW11 
  wstd_auth01W14 ~~ rvx4*wstd_auth01W14 
  wstd_eu_intW14 ~~ rvy4*wstd_eu_intW14 
  
  # Fix error variance of indicators to (1 - alpha)*var
std_auth01W10 ~~ 0.0058*std_auth01W10
std_auth01W11 ~~ 0.002*std_auth01W11
std_auth01W14 ~~ 0.00725*std_auth01W14
std_eu_intW10 ~~ 0.0028*std_eu_intW10
std_eu_intW11 ~~ 0.00194*std_eu_intW11
std_eu_intW14 ~~ 0*std_eu_intW14

  # Compute correlations of within-components at each wave
  cor2 := a*c + b*d + a*d*cor1 + b*c*cor1 + rcov2
  cor3 := a*c + b*d + a*d*cor2 + b*c*cor2 + rcov3
  cor4 := a*c + b*d + a*d*cor3 + b*c*cor3 + rcov4
 
  # Contrain residual variances of within-components such that variance of each 
  # within-component equals 1
  rvx2 == 1 - (a*a + b*b + 2*a*b*cor1)
  rvy2 == 1 - (c*c + d*d + 2*c*d*cor1)
  rvx3 == 1 - (a*a + b*b + 2*a*b*cor2)
  rvy3 == 1 - (c*c + d*d + 2*c*d*cor2)
  rvx4 == 1 - (a*a + b*b + 2*a*b*cor3)
  rvy4 == 1 - (c*c + d*d + 2*c*d*cor3)


'

eu_RICLPM_b_EQ.fit <- lavaan(eu_RICLPM_b_EQ, 
                             data = dat, 
                             estimator = "MLR",
                             missing = 'ML', 
                             meanstructure = T, 
                             int.ov.free = T) 
summary(eu_RICLPM_b_EQ.fit, standardized = T)

# Covariate

eu_RICLPM_tvc_EQ <- '
  # Create between components (random intercepts)
  RIstd_auth01W  =~ 1*std_auth01W7 + 1*std_auth01W10 + 1*std_auth01W11 + 1*std_auth01W14 
  RIstd_eu_intW =~ 1*std_eu_intW7 + 1*std_eu_intW10 + 1*std_eu_intW11 + 1*std_eu_intW14 
  
  # Create within-person centered variables
  wstd_auth01W7 =~ NA*std_auth01W7
  wstd_auth01W10 =~ NA*std_auth01W10
  wstd_auth01W11 =~ NA*std_auth01W11 
  wstd_auth01W14 =~ NA*std_auth01W14

  wstd_eu_intW7 =~ NA*std_eu_intW7
  wstd_eu_intW10 =~ NA*std_eu_intW10
  wstd_eu_intW11 =~ NA*std_eu_intW11
  wstd_eu_intW14 =~ NA*std_eu_intW14

  # Estimate the lagged effects between the within-person centered variables.
  wstd_auth01W10 ~ a*wstd_auth01W7 + b*wstd_eu_intW7
  wstd_eu_intW10 ~ c*wstd_auth01W7 + d*wstd_eu_intW7
  wstd_auth01W11 ~ a*wstd_auth01W10 + b*wstd_eu_intW10
  wstd_eu_intW11 ~ c*wstd_auth01W10 + d*wstd_eu_intW10
  wstd_auth01W14 ~ a*wstd_auth01W11 + b*wstd_eu_intW11
  wstd_eu_intW14 ~ c*wstd_auth01W11 + d*wstd_eu_intW11

  # Estimate the covariance between the within-person centered variables at the first wave. 
  wstd_auth01W7 ~~ cor1*wstd_eu_intW7 # Covariance
  
  # Label residual covariances
  wstd_auth01W10 ~~ rcov2*wstd_eu_intW10
  wstd_auth01W11 ~~ rcov3*wstd_eu_intW11
  wstd_auth01W14 ~~ rcov4*wstd_eu_intW14
  
  
  # Estimate the variance and covariance of the random intercepts. 
  RIstd_auth01W ~~ 0.729*RIstd_auth01W
  RIstd_eu_intW ~~ 0.841*RIstd_eu_intW
  RIstd_auth01W ~~ RIstd_eu_intW
  
  # Set variances of within-components at first wave to 1
  wstd_auth01W7 ~~ 1*wstd_auth01W7 # Variances
  wstd_eu_intW7 ~~ 1*wstd_eu_intW7 
  
  # Label residual variances
  wstd_auth01W10 ~~ rvx2*wstd_auth01W10 # Residual variances
  wstd_eu_intW10 ~~ rvy2*wstd_eu_intW10 
  wstd_auth01W11 ~~ rvx3*wstd_auth01W11
  wstd_eu_intW11 ~~ rvy3*wstd_eu_intW11 
  wstd_auth01W14 ~~ rvx4*wstd_auth01W14 
  wstd_eu_intW14 ~~ rvy4*wstd_eu_intW14 
  
  # Fix error variance of indicators to (1 - alpha)*var
std_auth01W10 ~~ 0.0058*std_auth01W10
std_auth01W11 ~~ 0.002*std_auth01W11
std_auth01W14 ~~ 0.00725*std_auth01W14
std_eu_intW10 ~~ 0.0028*std_eu_intW10
std_eu_intW11 ~~ 0.00194*std_eu_intW11
std_eu_intW14 ~~ 0*std_eu_intW14

  # Compute correlations of within-components at each wave
  cor2 := a*c + b*d + a*d*cor1 + b*c*cor1 + rcov2
  cor3 := a*c + b*d + a*d*cor2 + b*c*cor2 + rcov3
  cor4 := a*c + b*d + a*d*cor3 + b*c*cor3 + rcov4
 
  # Contrain residual variances of within-components such that variance of each 
  # within-component equals 1
  rvx2 == 1 - (a*a + b*b + 2*a*b*cor1)
  rvy2 == 1 - (c*c + d*d + 2*c*d*cor1)
  rvx3 == 1 - (a*a + b*b + 2*a*b*cor2)
  rvy3 == 1 - (c*c + d*d + 2*c*d*cor2)
  rvx4 == 1 - (a*a + b*b + 2*a*b*cor3)
  rvy4 == 1 - (c*c + d*d + 2*c*d*cor3)
  
  # incorporate tvc - age
wstd_auth01W7 + wstd_eu_intW7 ~ ageW7
wstd_auth01W10 + wstd_eu_intW10 ~ ageW10
wstd_auth01W11 + wstd_eu_intW11 ~ ageW11
wstd_auth01W14 + wstd_eu_intW14 ~ ageW14

ageW7 ~~ ageW7
ageW10 ~~ ageW10
ageW11 ~~ ageW11
ageW14 ~~ ageW14

ageW7 ~~ ageW10 + ageW11 + ageW14
ageW10 ~~ ageW11 + ageW14
ageW11 ~~ ageW14

# incorporate tvc – income
wstd_auth01W7 + wstd_eu_intW7 ~ incomerec7
wstd_auth01W10 + wstd_eu_intW10 ~ incomerec10
wstd_auth01W11 + wstd_eu_intW11 ~ incomerec11
wstd_auth01W14 + wstd_eu_intW14 ~ incomerec14

incomerec7 ~~ incomerec7
incomerec10 ~~ incomerec10
incomerec11 ~~ incomerec11
incomerec14 ~~ incomerec14

incomerec7 ~~ incomerec10 + incomerec11 + incomerec14
incomerec10 ~~ incomerec11 + incomerec14
incomerec11 ~~ incomerec14

# incorporate tvc – social grade
wstd_auth01W7 + wstd_eu_intW7 ~ p_socgradeW7
wstd_auth01W10 + wstd_eu_intW10 ~ p_socgradeW10
wstd_auth01W11 + wstd_eu_intW11 ~ p_socgradeW11
wstd_auth01W14 + wstd_eu_intW14 ~ p_socgradeW14

p_socgradeW7 ~~ p_socgradeW7
p_socgradeW10 ~~ p_socgradeW10
p_socgradeW11 ~~ p_socgradeW11
p_socgradeW14 ~~ p_socgradeW14

p_socgradeW7 ~~ p_socgradeW10 + p_socgradeW11 + p_socgradeW14
p_socgradeW10 ~~ p_socgradeW11 + p_socgradeW14
p_socgradeW11 ~~ p_socgradeW14
'

eu_RICLPM_tvc_EQ.fit <- lavaan(eu_RICLPM_tvc_EQ, 
                                   data = dat, 
                                   estimator = "MLR",
                                   missing = 'ML', 
                                   meanstructure = T, 
                                   int.ov.free = T) 
summary(eu_RICLPM_tvc_EQ.fit, standardized = T)

#### Equality constraints - CLPMs ####

## Immigration: 

# Baseline:

immig_pref_CLPM_EQ <- '
  # Create between components (random intercepts)
  RIauth01W  =~ 1*std_auth01W7 + 1*std_auth01W10 + 1*std_auth01W11 + 1*std_auth01W14 
  RIim_prefW =~ 1*std_im_pref_2W7 + 1*std_im_pref_2W10 + 1*std_im_pref_2W11 + 1*std_im_pref_2W14 
  
  # Create within-person centered variables
  wauth01W7 =~ NA*std_auth01W7
  wauth01W10 =~ NA*std_auth01W10
  wauth01W11 =~ NA*std_auth01W11 
  wauth01W14 =~ NA*std_auth01W14

  wim_prefW7 =~ NA*std_im_pref_2W7
  wim_prefW10 =~ NA*std_im_pref_2W10
  wim_prefW11 =~ NA*std_im_pref_2W11
  wim_prefW14 =~ NA*std_im_pref_2W14

  # Estimate the lagged effects between the within-person centered variables.
  wauth01W10 ~ a*wauth01W7 + b*wim_prefW7
  wim_prefW10 ~ c*wauth01W7 + d*wim_prefW7
  wauth01W11 ~ a*wauth01W10 + b*wim_prefW10
  wim_prefW11 ~ c*wauth01W10 + d*wim_prefW10
  wauth01W14 ~ a*wauth01W11 + b*wim_prefW11
  wim_prefW14 ~ c*wauth01W11 + d*wim_prefW11

  # Estimate the covariance between the within-person centered variables at the first wave. 
  wauth01W7 ~~ cor1*wim_prefW7 # Covariance
  
  # Label residual covariances
  wauth01W10 ~~ rcov2*wim_prefW10
  wauth01W11 ~~ rcov3*wim_prefW11
  wauth01W14 ~~ rcov4*wim_prefW14
  
  
  # Fix the variance and covariance of the random intercepts to zero 
  RIauth01W ~~ 0*RIauth01W
  RIim_prefW ~~ 0*RIim_prefW
  RIauth01W ~~ 0*RIim_prefW

  # Set variances of within-components at first wave to 1
  wauth01W7 ~~ 1*wauth01W7 # Variances
  wim_prefW7 ~~ 1*wim_prefW7
  
  wauth01W10 ~~ rvx2*wauth01W10 # Residual variances
  wim_prefW10 ~~ rvy2*wim_prefW10 
  wauth01W11 ~~ rvx3*wauth01W11
  wim_prefW11 ~~ rvy3*wim_prefW11 
  wauth01W14 ~~ rvx4*wauth01W14 
  wim_prefW14 ~~ rvy4*wim_prefW14 
  
# Fix error variance of indicators to (1 - alpha)*var
std_auth01W7 ~~ 0.02*std_auth01W7
std_auth01W10 ~~ 0.016*std_auth01W10
std_auth01W11 ~~ 0.018*std_auth01W11
std_auth01W14 ~~ 0.019*std_auth01W14
std_im_pref_2W7 ~~ 0.0147*std_im_pref_2W7
std_im_pref_2W10 ~~ 0.000155*std_im_pref_2W10
std_im_pref_2W11 ~~ 0.000147*std_im_pref_2W11
std_im_pref_2W14 ~~ 0.000512*std_im_pref_2W14

 # Compute correlations of within-components at each wave
  cor2 := a*c + b*d + a*d*cor1 + b*c*cor1 + rcov2
  cor3 := a*c + b*d + a*d*cor2 + b*c*cor2 + rcov3
  cor4 := a*c + b*d + a*d*cor3 + b*c*cor3 + rcov4
 
  # Contrain residual variances of within-components such that variance of each 
  # within-component equals 1
  rvx2 == 1 - (a*a + b*b + 2*a*b*cor1)
  rvy2 == 1 - (c*c + d*d + 2*c*d*cor1)
  rvx3 == 1 - (a*a + b*b + 2*a*b*cor2)
  rvy3 == 1 - (c*c + d*d + 2*c*d*cor2)
  rvx4 == 1 - (a*a + b*b + 2*a*b*cor3)
  rvy4 == 1 - (c*c + d*d + 2*c*d*cor3)

  '
immig_pref_CLPM_EQ.fit <- lavaan(immig_pref_CLPM_EQ,
                                 data = dat, 
                                 estimator = "MLR",
                                 missing = 'ML', 
                                 meanstructure = T, 
                                 int.ov.free = T) 
summary(immig_pref_CLPM_EQ.fit, standardized = T)

# Covariate:
immig_pref_CLPM_EQ_tvc <- '
  # Create between components (random intercepts)
  RIauth01W  =~ 1*std_auth01W7 + 1*std_auth01W10 + 1*std_auth01W11 + 1*std_auth01W14 
  RIim_prefW =~ 1*std_im_pref_2W7 + 1*std_im_pref_2W10 + 1*std_im_pref_2W11 + 1*std_im_pref_2W14 
  
  # Create within-person centered variables
  wauth01W7 =~ NA*std_auth01W7
  wauth01W10 =~ NA*std_auth01W10
  wauth01W11 =~ NA*std_auth01W11 
  wauth01W14 =~ NA*std_auth01W14

  wim_prefW7 =~ NA*std_im_pref_2W7
  wim_prefW10 =~ NA*std_im_pref_2W10
  wim_prefW11 =~ NA*std_im_pref_2W11
  wim_prefW14 =~ NA*std_im_pref_2W14

  # Estimate the lagged effects between the within-person centered variables.
  wauth01W10 ~ a*wauth01W7 + b*wim_prefW7
  wim_prefW10 ~ c*wauth01W7 + d*wim_prefW7
  wauth01W11 ~ a*wauth01W10 + b*wim_prefW10
  wim_prefW11 ~ c*wauth01W10 + d*wim_prefW10
  wauth01W14 ~ a*wauth01W11 + b*wim_prefW11
  wim_prefW14 ~ c*wauth01W11 + d*wim_prefW11

  # Estimate the covariance between the within-person centered variables at the first wave. 
  wauth01W7 ~~ cor1*wim_prefW7 # Covariance
  
  # Label residual covariances
  wauth01W10 ~~ rcov2*wim_prefW10
  wauth01W11 ~~ rcov3*wim_prefW11
  wauth01W14 ~~ rcov4*wim_prefW14
  
  
  # Fix the variance and covariance of the random intercepts to zero 
  RIauth01W ~~ 0*RIauth01W
  RIim_prefW ~~ 0*RIim_prefW
  RIauth01W ~~ 0*RIim_prefW

  # Set variances of within-components at first wave to 1
  wauth01W7 ~~ 1*wauth01W7 # Variances
  wim_prefW7 ~~ 1*wim_prefW7
  
  wauth01W10 ~~ rvx2*wauth01W10 # Residual variances
  wim_prefW10 ~~ rvy2*wim_prefW10 
  wauth01W11 ~~ rvx3*wauth01W11
  wim_prefW11 ~~ rvy3*wim_prefW11 
  wauth01W14 ~~ rvx4*wauth01W14 
  wim_prefW14 ~~ rvy4*wim_prefW14 
  
# Fix error variance of indicators to (1 - alpha)*var
std_auth01W7 ~~ 0.02*std_auth01W7
std_auth01W10 ~~ 0.016*std_auth01W10
std_auth01W11 ~~ 0.018*std_auth01W11
std_auth01W14 ~~ 0.019*std_auth01W14
std_im_pref_2W7 ~~ 0.0147*std_im_pref_2W7
std_im_pref_2W10 ~~ 0.000155*std_im_pref_2W10
std_im_pref_2W11 ~~ 0.000147*std_im_pref_2W11
std_im_pref_2W14 ~~ 0.000512*std_im_pref_2W14

 # Compute correlations of within-components at each wave
  cor2 := a*c + b*d + a*d*cor1 + b*c*cor1 + rcov2
  cor3 := a*c + b*d + a*d*cor2 + b*c*cor2 + rcov3
  cor4 := a*c + b*d + a*d*cor3 + b*c*cor3 + rcov4
 
  # Contrain residual variances of within-components such that variance of each 
  # within-component equals 1
  rvx2 == 1 - (a*a + b*b + 2*a*b*cor1)
  rvy2 == 1 - (c*c + d*d + 2*c*d*cor1)
  rvx3 == 1 - (a*a + b*b + 2*a*b*cor2)
  rvy3 == 1 - (c*c + d*d + 2*c*d*cor2)
  rvx4 == 1 - (a*a + b*b + 2*a*b*cor3)
  rvy4 == 1 - (c*c + d*d + 2*c*d*cor3)
  
  # incorporate tvc - age
wauth01W7 + wim_prefW7 ~ ageW7
wauth01W10 + wim_prefW10 ~ ageW10
wauth01W11 + wim_prefW11 ~ ageW11
wauth01W14 + wim_prefW14 ~ ageW14

ageW7 ~~ ageW7
ageW10 ~~ ageW10
ageW11 ~~ ageW11
ageW14 ~~ ageW14

ageW7 ~~ ageW10 + ageW11 + ageW14
ageW10 ~~ ageW11 + ageW14
ageW11 ~~ ageW14

# incorporate tvc - ethnicity
wauth01W7 + wim_prefW7 ~ ethnic7
wauth01W10 + wim_prefW10 ~ ethnic10
wauth01W11 + wim_prefW11 ~ ethnic11
wauth01W14 + wim_prefW14 ~ ethnic14

ethnic7 ~~ ethnic7
ethnic10 ~~ ethnic10
ethnic11 ~~ ethnic11
ethnic14 ~~ ethnic14

ethnic7 ~~ ethnic10 + ethnic11 + ethnic14
ethnic10 ~~ ethnic11 + ethnic14
ethnic11 ~~ ethnic14

# incorporate tvc – social grade
wauth01W7 + wim_prefW7 ~ p_socgradeW7
wauth01W10 + wim_prefW10 ~ p_socgradeW10
wauth01W11 + wim_prefW11 ~ p_socgradeW11
wauth01W14 + wim_prefW14 ~ p_socgradeW14

p_socgradeW7 ~~ p_socgradeW7
p_socgradeW10 ~~ p_socgradeW10
p_socgradeW11 ~~ p_socgradeW11
p_socgradeW14 ~~ p_socgradeW14

p_socgradeW7 ~~ p_socgradeW10 + p_socgradeW11 + p_socgradeW14
p_socgradeW10 ~~ p_socgradeW11 + p_socgradeW14
p_socgradeW11 ~~ p_socgradeW14

# incorporate tvc – education
wauth01W7 + wim_prefW7 ~ higheduc7
wauth01W10 + wim_prefW10 ~ higheduc10
wauth01W11 + wim_prefW11 ~ higheduc11
wauth01W14 + wim_prefW14 ~ higheduc14

higheduc7 ~~ higheduc7
higheduc10 ~~ higheduc10
higheduc11 ~~ higheduc11
higheduc14 ~~ higheduc14

higheduc7 ~~ higheduc10 + higheduc11 + higheduc14
higheduc10 ~~ higheduc11 + higheduc14
higheduc11 ~~ higheduc14

# incorporate tvc – income
wauth01W7 + wim_prefW7 ~ incomerec7
wauth01W10 + wim_prefW10 ~ incomerec10
wauth01W11 + wim_prefW11 ~ incomerec11
wauth01W14 + wim_prefW14 ~ incomerec14

incomerec7 ~~ incomerec7
incomerec10 ~~ incomerec10
incomerec11 ~~ incomerec11
incomerec14 ~~ incomerec14

incomerec7 ~~ incomerec10 + incomerec11 + incomerec14
incomerec10 ~~ incomerec11 + incomerec14
incomerec11 ~~ incomerec14

  '
immig_pref_CLPM_EQ_tvc.fit <- lavaan(immig_pref_CLPM_EQ_tvc,
                                     data = dat, 
                                     estimator = "MLR",
                                     missing = 'ML', 
                                     meanstructure = T, 
                                     int.ov.free = T) 
summary(immig_pref_CLPM_EQ_tvc.fit, standardized = T)

## Redistribution 

# Baseline 

redist_CLPM_b_EQ <- '
  # Create between components (random intercepts)
  RIstd_auth01W  =~ 1*std_auth01W7 + 1*std_auth01W10 + 1*std_auth01W11 + 1*std_auth01W14 
  RIstd_redist_prefW =~ 1*std_redistSelf01W7 + 1*std_redistSelf01W10 + 1*std_redistSelf01W11 + 1*std_redistSelf01W14 
  
  # Create within-person centered variables
  wstd_auth01W7 =~ NA*std_auth01W7
  wstd_auth01W10 =~ NA*std_auth01W10
  wstd_auth01W11 =~ NA*std_auth01W11 
  wstd_auth01W14 =~ NA*std_auth01W14

  wstd_redist_prefW7 =~ NA*std_redistSelf01W7
  wstd_redist_prefW10 =~ NA*std_redistSelf01W10
  wstd_redist_prefW11 =~ NA*std_redistSelf01W11
  wstd_redist_prefW14 =~ NA*std_redistSelf01W14

  # Estimate the lagged effects between the within-person centered variables.
  wstd_auth01W10 ~ a*wstd_auth01W7 + b*wstd_redist_prefW7
  wstd_redist_prefW10 ~ c*wstd_auth01W7 + d*wstd_redist_prefW7
  wstd_auth01W11 ~ a*wstd_auth01W10 + b*wstd_redist_prefW10
  wstd_redist_prefW11 ~ c*wstd_auth01W10 + d*wstd_redist_prefW10
  wstd_auth01W14 ~ a*wstd_auth01W11 + b*wstd_redist_prefW11
  wstd_redist_prefW14 ~ c*wstd_auth01W11 + d*wstd_redist_prefW11

  # Estimate the covariance between the within-person centered variables at the first wave. 
  wstd_auth01W7 ~~ cor1*wstd_redist_prefW7 # Covariance
  
  # Estimate the covariances between the residuals of the within-person centered variables (the innovations).
  wstd_auth01W10 ~~ rcov2*wstd_redist_prefW10
  wstd_auth01W11 ~~ rcov3*wstd_redist_prefW11
  wstd_auth01W14 ~~ rcov4*wstd_redist_prefW14
  
  
  # Fix the variance and covariance of the random interceptsto 0 
  RIstd_auth01W ~~ 0*RIstd_auth01W
  RIstd_redist_prefW ~~ 0*RIstd_redist_prefW
  RIstd_auth01W ~~ 0*RIstd_redist_prefW

  # Set variances of within-components at first wave to 1
  wstd_auth01W7 ~~ 1*wstd_auth01W7 # Variances
  wstd_redist_prefW7 ~~ 1*wstd_redist_prefW7 
  
  # Label residual variances
  wstd_auth01W10 ~~ rvx2*wstd_auth01W10 # Residual variances
  wstd_redist_prefW10 ~~ rvy2*wstd_redist_prefW10 
  wstd_auth01W11 ~~ rvx3*wstd_auth01W11
  wstd_redist_prefW11 ~~ rvy3*wstd_redist_prefW11 
  wstd_auth01W14 ~~ rvx4*wstd_auth01W14 
  wstd_redist_prefW14 ~~ rvy4*wstd_redist_prefW14 
  
  # Fix error variance of indicators to (1 - alpha)*var
std_auth01W10 ~~ 0.0058*std_auth01W10
std_auth01W11 ~~ 0.002*std_auth01W11
std_auth01W14 ~~ 0.00725*std_auth01W14
std_redistSelf01W10 ~~ 0.01038*std_redistSelf01W10
std_redistSelf01W11 ~~ 0.00933*std_redistSelf01W11
std_redistSelf01W14 ~~ 0*std_redistSelf01W14

 # Compute correlations of within-components at each wave
  cor2 := a*c + b*d + a*d*cor1 + b*c*cor1 + rcov2
  cor3 := a*c + b*d + a*d*cor2 + b*c*cor2 + rcov3
  cor4 := a*c + b*d + a*d*cor3 + b*c*cor3 + rcov4
 
  # Contrain residual variances of within-components such that variance of each 
  # within-component equals 1
  rvx2 == 1 - (a*a + b*b + 2*a*b*cor1)
  rvy2 == 1 - (c*c + d*d + 2*c*d*cor1)
  rvx3 == 1 - (a*a + b*b + 2*a*b*cor2)
  rvy3 == 1 - (c*c + d*d + 2*c*d*cor2)
  rvx4 == 1 - (a*a + b*b + 2*a*b*cor3)
  rvy4 == 1 - (c*c + d*d + 2*c*d*cor3)

'

redist_CLPM_b_EQ.fit <- lavaan(redist_CLPM_b_EQ, 
                               data = dat, 
                               estimator = "MLR",
                               missing = 'ML',
                               meanstructure = T, int.ov.free = T) 
summary(redist_CLPM_b_EQ.fit, standardized = T)

# Covariate

redist_CLPM_tvc_EQ <- '
  # Create between components (random intercepts)
  RIstd_auth01W  =~ 1*std_auth01W7 + 1*std_auth01W10 + 1*std_auth01W11 + 1*std_auth01W14 
  RIstd_redist_prefW =~ 1*std_redistSelf01W7 + 1*std_redistSelf01W10 + 1*std_redistSelf01W11 + 1*std_redistSelf01W14 
  
  # Create within-person centered variables
  wstd_auth01W7 =~ NA*std_auth01W7
  wstd_auth01W10 =~ NA*std_auth01W10
  wstd_auth01W11 =~ NA*std_auth01W11 
  wstd_auth01W14 =~ NA*std_auth01W14

  wstd_redist_prefW7 =~ NA*std_redistSelf01W7
  wstd_redist_prefW10 =~ NA*std_redistSelf01W10
  wstd_redist_prefW11 =~ NA*std_redistSelf01W11
  wstd_redist_prefW14 =~ NA*std_redistSelf01W14

  # Estimate the lagged effects between the within-person centered variables.
  wstd_auth01W10 ~ a*wstd_auth01W7 + b*wstd_redist_prefW7
  wstd_redist_prefW10 ~ c*wstd_auth01W7 + d*wstd_redist_prefW7
  wstd_auth01W11 ~ a*wstd_auth01W10 + b*wstd_redist_prefW10
  wstd_redist_prefW11 ~ c*wstd_auth01W10 + d*wstd_redist_prefW10
  wstd_auth01W14 ~ a*wstd_auth01W11 + b*wstd_redist_prefW11
  wstd_redist_prefW14 ~ c*wstd_auth01W11 + d*wstd_redist_prefW11

  # Estimate the covariance between the within-person centered variables at the first wave. 
  wstd_auth01W7 ~~ cor1*wstd_redist_prefW7 # Covariance
  
  # Estimate the covariances between the residuals of the within-person centered variables (the innovations).
  wstd_auth01W10 ~~ rcov2*wstd_redist_prefW10
  wstd_auth01W11 ~~ rcov3*wstd_redist_prefW11
  wstd_auth01W14 ~~ rcov4*wstd_redist_prefW14
  
  # Fix the variance and covariance of the random interceptsto 0 
  RIstd_auth01W ~~ 0*RIstd_auth01W
  RIstd_redist_prefW ~~ 0*RIstd_redist_prefW
  RIstd_auth01W ~~ 0*RIstd_redist_prefW

  # Set variances of within-components at first wave to 1
  wstd_auth01W7 ~~ 1*wstd_auth01W7 # Variances
  wstd_redist_prefW7 ~~ 1*wstd_redist_prefW7 
  
  # Label residual variances
  wstd_auth01W10 ~~ rvx2*wstd_auth01W10 # Residual variances
  wstd_redist_prefW10 ~~ rvy2*wstd_redist_prefW10 
  wstd_auth01W11 ~~ rvx3*wstd_auth01W11
  wstd_redist_prefW11 ~~ rvy3*wstd_redist_prefW11 
  wstd_auth01W14 ~~ rvx4*wstd_auth01W14 
  wstd_redist_prefW14 ~~ rvy4*wstd_redist_prefW14 
  
  # Fix error variance of indicators to (1 - alpha)*var
std_auth01W10 ~~ 0.0058*std_auth01W10
std_auth01W11 ~~ 0.002*std_auth01W11
std_auth01W14 ~~ 0.00725*std_auth01W14
std_redistSelf01W10 ~~ 0.01038*std_redistSelf01W10
std_redistSelf01W11 ~~ 0.00933*std_redistSelf01W11
std_redistSelf01W14 ~~ 0*std_redistSelf01W14

 # Compute correlations of within-components at each wave
  cor2 := a*c + b*d + a*d*cor1 + b*c*cor1 + rcov2
  cor3 := a*c + b*d + a*d*cor2 + b*c*cor2 + rcov3
  cor4 := a*c + b*d + a*d*cor3 + b*c*cor3 + rcov4
 
  # Contrain residual variances of within-components such that variance of each 
  # within-component equals 1
  rvx2 == 1 - (a*a + b*b + 2*a*b*cor1)
  rvy2 == 1 - (c*c + d*d + 2*c*d*cor1)
  rvx3 == 1 - (a*a + b*b + 2*a*b*cor2)
  rvy3 == 1 - (c*c + d*d + 2*c*d*cor2)
  rvx4 == 1 - (a*a + b*b + 2*a*b*cor3)
  rvy4 == 1 - (c*c + d*d + 2*c*d*cor3)
  
  # incorporate tvc - age
wstd_auth01W7 + wstd_redist_prefW7 ~ ageW7
wstd_auth01W10 + wstd_redist_prefW10 ~ ageW10
wstd_auth01W11 + wstd_redist_prefW11 ~ ageW11
wstd_auth01W14 + wstd_redist_prefW14 ~ ageW14

ageW7 ~~ ageW7
ageW10 ~~ ageW10
ageW11 ~~ ageW11
ageW14 ~~ ageW14

ageW7 ~~ ageW10 + ageW11 + ageW14
ageW10 ~~ ageW11 + ageW14
ageW11 ~~ ageW14

# incorporate tvc - ethnicity
wstd_auth01W7 + wstd_redist_prefW7 ~ ethnic7
wstd_auth01W10 + wstd_redist_prefW10 ~ ethnic10
wstd_auth01W11 + wstd_redist_prefW11 ~ ethnic11
wstd_auth01W14 + wstd_redist_prefW14 ~ ethnic14

ethnic7 ~~ ethnic7
ethnic10 ~~ ethnic10
ethnic11 ~~ ethnic11
ethnic14 ~~ ethnic14

ethnic7 ~~ ethnic10 + ethnic11 + ethnic14
ethnic10 ~~ ethnic11 + ethnic14
ethnic11 ~~ ethnic14

# incorporate tvc – social grade
wstd_auth01W7 + wstd_redist_prefW7 ~ p_socgradeW7
wstd_auth01W10 + wstd_redist_prefW10 ~ p_socgradeW10
wstd_auth01W11 + wstd_redist_prefW11 ~ p_socgradeW11
wstd_auth01W14 + wstd_redist_prefW14 ~ p_socgradeW14

p_socgradeW7 ~~ p_socgradeW7
p_socgradeW10 ~~ p_socgradeW10
p_socgradeW11 ~~ p_socgradeW11
p_socgradeW14 ~~ p_socgradeW14

p_socgradeW7 ~~ p_socgradeW10 + p_socgradeW11 + p_socgradeW14
p_socgradeW10 ~~ p_socgradeW11 + p_socgradeW14
p_socgradeW11 ~~ p_socgradeW14

# incorporate tvc – education
wstd_auth01W7 + wstd_redist_prefW7 ~ higheduc7
wstd_auth01W10 + wstd_redist_prefW10 ~ higheduc10
wstd_auth01W11 + wstd_redist_prefW11 ~ higheduc11
wstd_auth01W14 + wstd_redist_prefW14 ~ higheduc14

higheduc7 ~~ higheduc7
higheduc10 ~~ higheduc10
higheduc11 ~~ higheduc11
higheduc14 ~~ higheduc14

higheduc7 ~~ higheduc10 + higheduc11 + higheduc14
higheduc10 ~~ higheduc11 + higheduc14
higheduc11 ~~ higheduc14

# incorporate tvc – income
wstd_auth01W7 + wstd_redist_prefW7 ~ incomerec7
wstd_auth01W10 + wstd_redist_prefW10 ~ incomerec10
wstd_auth01W11 + wstd_redist_prefW11 ~ incomerec11
wstd_auth01W14 + wstd_redist_prefW14 ~ incomerec14

incomerec7 ~~ incomerec7
incomerec10 ~~ incomerec10
incomerec11 ~~ incomerec11
incomerec14 ~~ incomerec14

incomerec7 ~~ incomerec10 + incomerec11 + incomerec14
incomerec10 ~~ incomerec11 + incomerec14
incomerec11 ~~ incomerec14

'

redist_CLPM_tvc_EQ.fit <- lavaan(redist_CLPM_tvc_EQ, 
                                 data = dat, 
                                 estimator = "MLR",
                                 missing = 'ML',
                                 meanstructure = T, int.ov.free = T) 
summary(redist_CLPM_tvc_EQ.fit, standardized = T)

## EU integration

eu_CLPM_b_EQ <- '
  # Create between components (random intercepts)
  RIstd_auth01W  =~ 1*std_auth01W7 + 1*std_auth01W10 + 1*std_auth01W11 + 1*std_auth01W14 
  RIstd_eu_intW =~ 1*std_eu_intW7 + 1*std_eu_intW10 + 1*std_eu_intW11 + 1*std_eu_intW14 
  
  # Create within-person centered variables
  wstd_auth01W7 =~ NA*std_auth01W7
  wstd_auth01W10 =~ NA*std_auth01W10
  wstd_auth01W11 =~ NA*std_auth01W11 
  wstd_auth01W14 =~ NA*std_auth01W14

  wstd_eu_intW7 =~ NA*std_eu_intW7
  wstd_eu_intW10 =~ NA*std_eu_intW10
  wstd_eu_intW11 =~ NA*std_eu_intW11
  wstd_eu_intW14 =~ NA*std_eu_intW14

  # Estimate the lagged effects between the within-person centered variables.
  wstd_auth01W10 ~ a*wstd_auth01W7 + b*wstd_eu_intW7
  wstd_eu_intW10 ~ c*wstd_auth01W7 + d*wstd_eu_intW7
  wstd_auth01W11 ~ a*wstd_auth01W10 + b*wstd_eu_intW10
  wstd_eu_intW11 ~ c*wstd_auth01W10 + d*wstd_eu_intW10
  wstd_auth01W14 ~ a*wstd_auth01W11 + b*wstd_eu_intW11
  wstd_eu_intW14 ~ c*wstd_auth01W11 + d*wstd_eu_intW11

  # Estimate the covariance between the within-person centered variables at the first wave. 
  wstd_auth01W7 ~~ cor1*wstd_eu_intW7 # Covariance
  
  # Label residual covariances
  wstd_auth01W10 ~~ rcov2*wstd_eu_intW10
  wstd_auth01W11 ~~ rcov3*wstd_eu_intW11
  wstd_auth01W14 ~~ rcov4*wstd_eu_intW14
  
  
  # Fix the variance and covariance of the random intercepts to zero
  RIstd_auth01W ~~ 0*RIstd_auth01W
  RIstd_eu_intW ~~ 0*RIstd_eu_intW
  RIstd_auth01W ~~ 0*RIstd_eu_intW
  
  # Set variances of within-components at first wave to 1
  wstd_auth01W7 ~~ 1*wstd_auth01W7 # Variances
  wstd_eu_intW7 ~~ 1*wstd_eu_intW7 
  
  # Label residual variances
  wstd_auth01W10 ~~ rvx2*wstd_auth01W10 # Residual variances
  wstd_eu_intW10 ~~ rvy2*wstd_eu_intW10 
  wstd_auth01W11 ~~ rvx3*wstd_auth01W11
  wstd_eu_intW11 ~~ rvy3*wstd_eu_intW11 
  wstd_auth01W14 ~~ rvx4*wstd_auth01W14 
  wstd_eu_intW14 ~~ rvy4*wstd_eu_intW14 
  
  # Fix error variance of indicators to (1 - alpha)*var
std_auth01W10 ~~ 0.0058*std_auth01W10
std_auth01W11 ~~ 0.002*std_auth01W11
std_auth01W14 ~~ 0.00725*std_auth01W14
std_eu_intW10 ~~ 0.0028*std_eu_intW10
std_eu_intW11 ~~ 0.00194*std_eu_intW11
std_eu_intW14 ~~ 0*std_eu_intW14

  # Compute correlations of within-components at each wave
  cor2 := a*c + b*d + a*d*cor1 + b*c*cor1 + rcov2
  cor3 := a*c + b*d + a*d*cor2 + b*c*cor2 + rcov3
  cor4 := a*c + b*d + a*d*cor3 + b*c*cor3 + rcov4
 
  # Contrain residual variances of within-components such that variance of each 
  # within-component equals 1
  rvx2 == 1 - (a*a + b*b + 2*a*b*cor1)
  rvy2 == 1 - (c*c + d*d + 2*c*d*cor1)
  rvx3 == 1 - (a*a + b*b + 2*a*b*cor2)
  rvy3 == 1 - (c*c + d*d + 2*c*d*cor2)
  rvx4 == 1 - (a*a + b*b + 2*a*b*cor3)
  rvy4 == 1 - (c*c + d*d + 2*c*d*cor3)


'

eu_CLPM_b_EQ.fit <- lavaan(eu_CLPM_b_EQ, 
                           data = dat, 
                           estimator = "MLR",
                           missing = 'ML', 
                           meanstructure = T, 
                           int.ov.free = T) 
summary(eu_CLPM_b_EQ.fit, standardized = T)

# Covariate

eu_CLPM_tvc_EQ <- '
  # Create between components (random intercepts)
  RIstd_auth01W  =~ 1*std_auth01W7 + 1*std_auth01W10 + 1*std_auth01W11 + 1*std_auth01W14 
  RIstd_eu_intW =~ 1*std_eu_intW7 + 1*std_eu_intW10 + 1*std_eu_intW11 + 1*std_eu_intW14 
  
  # Create within-person centered variables
  wstd_auth01W7 =~ NA*std_auth01W7
  wstd_auth01W10 =~ NA*std_auth01W10
  wstd_auth01W11 =~ NA*std_auth01W11 
  wstd_auth01W14 =~ NA*std_auth01W14

  wstd_eu_intW7 =~ NA*std_eu_intW7
  wstd_eu_intW10 =~ NA*std_eu_intW10
  wstd_eu_intW11 =~ NA*std_eu_intW11
  wstd_eu_intW14 =~ NA*std_eu_intW14

  # Estimate the lagged effects between the within-person centered variables.
  wstd_auth01W10 ~ a*wstd_auth01W7 + b*wstd_eu_intW7
  wstd_eu_intW10 ~ c*wstd_auth01W7 + d*wstd_eu_intW7
  wstd_auth01W11 ~ a*wstd_auth01W10 + b*wstd_eu_intW10
  wstd_eu_intW11 ~ c*wstd_auth01W10 + d*wstd_eu_intW10
  wstd_auth01W14 ~ a*wstd_auth01W11 + b*wstd_eu_intW11
  wstd_eu_intW14 ~ c*wstd_auth01W11 + d*wstd_eu_intW11

  # Estimate the covariance between the within-person centered variables at the first wave. 
  wstd_auth01W7 ~~ cor1*wstd_eu_intW7 # Covariance
  
  # Label residual covariances
  wstd_auth01W10 ~~ rcov2*wstd_eu_intW10
  wstd_auth01W11 ~~ rcov3*wstd_eu_intW11
  wstd_auth01W14 ~~ rcov4*wstd_eu_intW14
  
  
  # Fix the variance and covariance of the random intercepts to zero
  RIstd_auth01W ~~ 0*RIstd_auth01W
  RIstd_eu_intW ~~ 0*RIstd_eu_intW
  RIstd_auth01W ~~ 0*RIstd_eu_intW
  
  # Set variances of within-components at first wave to 1
  wstd_auth01W7 ~~ 1*wstd_auth01W7 # Variances
  wstd_eu_intW7 ~~ 1*wstd_eu_intW7 
  
  # Label residual variances
  wstd_auth01W10 ~~ rvx2*wstd_auth01W10 # Residual variances
  wstd_eu_intW10 ~~ rvy2*wstd_eu_intW10 
  wstd_auth01W11 ~~ rvx3*wstd_auth01W11
  wstd_eu_intW11 ~~ rvy3*wstd_eu_intW11 
  wstd_auth01W14 ~~ rvx4*wstd_auth01W14 
  wstd_eu_intW14 ~~ rvy4*wstd_eu_intW14 
  
  # Fix error variance of indicators to (1 - alpha)*var
std_auth01W10 ~~ 0.0058*std_auth01W10
std_auth01W11 ~~ 0.002*std_auth01W11
std_auth01W14 ~~ 0.00725*std_auth01W14
std_eu_intW10 ~~ 0.0028*std_eu_intW10
std_eu_intW11 ~~ 0.00194*std_eu_intW11
std_eu_intW14 ~~ 0*std_eu_intW14

  # Compute correlations of within-components at each wave
  cor2 := a*c + b*d + a*d*cor1 + b*c*cor1 + rcov2
  cor3 := a*c + b*d + a*d*cor2 + b*c*cor2 + rcov3
  cor4 := a*c + b*d + a*d*cor3 + b*c*cor3 + rcov4
 
  # Contrain residual variances of within-components such that variance of each 
  # within-component equals 1
  rvx2 == 1 - (a*a + b*b + 2*a*b*cor1)
  rvy2 == 1 - (c*c + d*d + 2*c*d*cor1)
  rvx3 == 1 - (a*a + b*b + 2*a*b*cor2)
  rvy3 == 1 - (c*c + d*d + 2*c*d*cor2)
  rvx4 == 1 - (a*a + b*b + 2*a*b*cor3)
  rvy4 == 1 - (c*c + d*d + 2*c*d*cor3)

# incorporate tvc - age
wstd_auth01W7 + wstd_eu_intW7 ~ ageW7
wstd_auth01W10 + wstd_eu_intW10 ~ ageW10
wstd_auth01W11 + wstd_eu_intW11 ~ ageW11
wstd_auth01W14 + wstd_eu_intW14 ~ ageW14

ageW7 ~~ ageW7
ageW10 ~~ ageW10
ageW11 ~~ ageW11
ageW14 ~~ ageW14

ageW7 ~~ ageW10 + ageW11 + ageW14
ageW10 ~~ ageW11 + ageW14
ageW11 ~~ ageW14

# incorporate tvc - ethnicity
wstd_auth01W7 + wstd_eu_intW7 ~ ethnic7
wstd_auth01W10 + wstd_eu_intW10 ~ ethnic10
wstd_auth01W11 + wstd_eu_intW11 ~ ethnic11
wstd_auth01W14 + wstd_eu_intW14 ~ ethnic14

ethnic7 ~~ ethnic7
ethnic10 ~~ ethnic10
ethnic11 ~~ ethnic11
ethnic14 ~~ ethnic14

ethnic7 ~~ ethnic10 + ethnic11 + ethnic14
ethnic10 ~~ ethnic11 + ethnic14
ethnic11 ~~ ethnic14

# incorporate tvc – social grade
wstd_auth01W7 + wstd_eu_intW7 ~ p_socgradeW7
wstd_auth01W10 + wstd_eu_intW10 ~ p_socgradeW10
wstd_auth01W11 + wstd_eu_intW11 ~ p_socgradeW11
wstd_auth01W14 + wstd_eu_intW14 ~ p_socgradeW14

p_socgradeW7 ~~ p_socgradeW7
p_socgradeW10 ~~ p_socgradeW10
p_socgradeW11 ~~ p_socgradeW11
p_socgradeW14 ~~ p_socgradeW14

p_socgradeW7 ~~ p_socgradeW10 + p_socgradeW11 + p_socgradeW14
p_socgradeW10 ~~ p_socgradeW11 + p_socgradeW14
p_socgradeW11 ~~ p_socgradeW14

# incorporate tvc – education
wstd_auth01W7 + wstd_eu_intW7 ~ higheduc7
wstd_auth01W10 + wstd_eu_intW10 ~ higheduc10
wstd_auth01W11 + wstd_eu_intW11 ~ higheduc11
wstd_auth01W14 + wstd_eu_intW14 ~ higheduc14

higheduc7 ~~ higheduc7
higheduc10 ~~ higheduc10
higheduc11 ~~ higheduc11
higheduc14 ~~ higheduc14

higheduc7 ~~ higheduc10 + higheduc11 + higheduc14
higheduc10 ~~ higheduc11 + higheduc14
higheduc11 ~~ higheduc14

# incorporate tvc – income
wstd_auth01W7 + wstd_eu_intW7 ~ incomerec7
wstd_auth01W10 + wstd_eu_intW10 ~ incomerec10
wstd_auth01W11 + wstd_eu_intW11 ~ incomerec11
wstd_auth01W14 + wstd_eu_intW14 ~ incomerec14

incomerec7 ~~ incomerec7
incomerec10 ~~ incomerec10
incomerec11 ~~ incomerec11
incomerec14 ~~ incomerec14

incomerec7 ~~ incomerec10 + incomerec11 + incomerec14
incomerec10 ~~ incomerec11 + incomerec14
incomerec11 ~~ incomerec14

'

eu_CLPM_tvc_EQ.fit <- lavaan(eu_CLPM_tvc_EQ, 
                             data = dat, 
                             estimator = "MLR",
                             missing = 'ML', 
                             meanstructure = T, 
                             int.ov.free = T) 
summary(eu_CLPM_tvc_EQ.fit, standardized = T)


#### Model fit - RI-CLPMs #####

fitmeasures(std_immig_pref_b.fit)
fitmeasures(std_immig_pref_tvc.fit)

fitmeasures(std_redist_pref.fit)
fitmeasures(std_redist_pref_tvc.fit)

fitmeasures(std_eu_pref_b.fit)
fitmeasures(std_eu_pref_tvc.fit)

#### RI-CLPMs with EQ constraints

fitmeasures(immig_pref_RICLPM_EQ.fit)
fitmeasures(immig_pref_2_tvc_EQ.fit)

fitmeasures(redist_RICLPM_b_EQ.fit)
fitmeasures(redist_RICLPM_tvc_EQ.fit)

fitmeasures(eu_RICLPM_b_EQ.fit)
fitmeasures(eu_RICLPM_tvc_EQ.fit)

#### Model fit - CLPMs ####

fitmeasures(std_immig_pref_CLPM_b.fit)
fitmeasures(std_immig_pref_CLPM_tvc.fit)

fitmeasures(std_redist_pref_CLPM_b.fit)
fitmeasures(std_redist_pref_CLPM_tvc.fit)

fitmeasures(std_eu_pref_CLPM_b.fit)
fitmeasures(std_eu_pref_CLPM_tvc.fit)

#### CLPMs with EQ constraints

fitmeasures(immig_pref_CLPM_EQ.fit)
fitmeasures(immig_pref_CLPM_EQ_tvc.fit)

fitmeasures(redist_CLPM_b_EQ.fit)
fitmeasures(redist_CLPM_tvc_EQ.fit)

fitmeasures(eu_CLPM_b_EQ.fit)
fitmeasures(eu_CLPM_tvc_EQ.fit)

#### Results - RI-CLPMs ####

standardizedsolution(std_immig_pref_b.fit)
standardizedsolution(std_immig_pref_tvc.fit)

standardizedsolution(std_redist_pref.fit)
standardizedsolution(std_redist_pref_tvc.fit)

standardizedsolution(std_eu_pref_b.fit)
standardizedsolution(std_eu_pref_tvc.fit)

#### RI-CLPMs with EQ constraints

standardizedsolution(immig_pref_RICLPM_EQ.fit)
standardizedsolution(immig_pref_2_tvc_EQ.fit)

standardizedsolution(redist_RICLPM_b_EQ.fit)
standardizedsolution(redist_RICLPM_tvc_EQ.fit)

standardizedsolution(eu_RICLPM_b_EQ.fit)
standardizedsolution(eu_RICLPM_tvc_EQ.fit)


#### Results - CLPMs ####

standardizedsolution(std_immig_pref_CLPM_b.fit)
standardizedsolution(std_immig_pref_CLPM_tvc.fit)

standardizedsolution(std_redist_pref_CLPM_b.fit)
standardizedsolution(std_redist_pref_CLPM_tvc.fit)

standardizedsolution(std_eu_pref_CLPM_b.fit)
standardizedsolution(std_eu_pref_CLPM_tvc.fit)

#### CLPMs with EQ constraints

standardizedsolution(immig_pref_CLPM_EQ.fit)
standardizedsolution(immig_pref_CLPM_EQ_tvc.fit)

standardizedsolution(redist_CLPM_b_EQ.fit)
standardizedsolution(redist_CLPM_tvc_EQ.fit)

standardizedsolution(eu_CLPM_b_EQ.fit)
standardizedsolution(eu_CLPM_tvc_EQ.fit)

#### Multiple-group RI-CLPMs - Engagement ####

att_dat <- read.dta13("C:/Replication files_Authoritarianism, Political Attitudes and Vote Choice/RICLPM data_attention.dta")

att_dat_high <- read.dta13("C:/Replication files_Authoritarianism, Political Attitudes and Vote Choice/RICLPM data_attention_high.dta")

att_dat_low <- read.dta13("C:/Replication files_Authoritarianism, Political Attitudes and Vote Choice/RICLPM data_attention_low.dta")
## Redistribution 

std_redist_attU <- '
  # Create between components (random intercepts)
  RIstd_auth01W  =~ 1*std_auth01W7 + 1*std_auth01W10 + 1*std_auth01W11 + 1*std_auth01W14 
  RIstd_redist_prefW =~ 1*std_redistSelf01W7 + 1*std_redistSelf01W10 + 1*std_redistSelf01W11 + 1*std_redistSelf01W14 
  
  # Create within-person centered variables
  wstd_auth01W7 =~ 1*std_auth01W7
  wstd_auth01W10 =~ 1*std_auth01W10
  wstd_auth01W11 =~ 1*std_auth01W11 
  wstd_auth01W14 =~ 1*std_auth01W14

  wstd_redist_prefW7 =~ 1*std_redistSelf01W7
  wstd_redist_prefW10 =~ 1*std_redistSelf01W10
  wstd_redist_prefW11 =~ 1*std_redistSelf01W11
  wstd_redist_prefW14 =~ 1*std_redistSelf01W14

  # Estimate the lagged effects between the within-person centered variables.
  wstd_auth01W10 + wstd_redist_prefW10 ~ wstd_auth01W7 + wstd_redist_prefW7
  wstd_auth01W11 + wstd_redist_prefW11 ~ wstd_auth01W10 + wstd_redist_prefW10
  wstd_auth01W14 + wstd_redist_prefW14 ~ wstd_auth01W11 + wstd_redist_prefW11

  # Estimate the covariance between the within-person centered variables at the first wave. 
  wstd_auth01W7 ~~ wstd_redist_prefW7 # Covariance
  
  # Estimate the covariances between the residuals of the within-person centered variables (the innovations).
  wstd_auth01W10 ~~ wstd_redist_prefW10
  wstd_auth01W11 ~~ wstd_redist_prefW11
  wstd_auth01W14 ~~ wstd_redist_prefW14
  
  
  # Estimate the variance and covariance of the random intercepts. 
  RIstd_auth01W ~~ RIstd_auth01W
  RIstd_redist_prefW ~~ RIstd_redist_prefW
  RIstd_auth01W ~~ RIstd_redist_prefW

  # Estimate the (residual) variance of the within-person centered variables.
  wstd_auth01W7 ~~ wstd_auth01W7 # Variances
  wstd_redist_prefW7 ~~ wstd_redist_prefW7 
  wstd_auth01W10 ~~ wstd_auth01W10 # Residual variances
  wstd_redist_prefW10 ~~ wstd_redist_prefW10 
  wstd_auth01W11 ~~ wstd_auth01W11
  wstd_redist_prefW11 ~~ wstd_redist_prefW11 
  wstd_auth01W14 ~~ wstd_auth01W14 
  wstd_redist_prefW14 ~~ wstd_redist_prefW14 
  
  # Fix error variance of indicators to (1 - alpha)*var
std_auth01W10 ~~ 0.0058*std_auth01W10
std_auth01W11 ~~ 0.002*std_auth01W11
std_auth01W14 ~~ 0.00725*std_auth01W14
std_redistSelf01W10 ~~ 0.01038*std_redistSelf01W10
std_redistSelf01W11 ~~ 0.00933*std_redistSelf01W11
std_redistSelf01W14 ~~ 0*std_redistSelf01W14

'

std_redist_attU.fit <- lavaan(std_redist_attU, 
                              data = att_dat,
                              group = "polatt_group",
                              estimator = "MLR",
                              missing = 'ML', 
                              meanstructure = T, 
                              int.ov.free = T) 
summary(std_redist_attU.fit, standardized = T)

std_redist_attC <- '
  # Create between components (random intercepts)
  RIstd_auth01W  =~ 1*std_auth01W7 + 1*std_auth01W10 + 1*std_auth01W11 + 1*std_auth01W14 
  RIstd_redist_prefW =~ 1*std_redistSelf01W7 + 1*std_redistSelf01W10 + 1*std_redistSelf01W11 + 1*std_redistSelf01W14 
  
  # Create within-person centered variables
  wstd_auth01W7 =~ 1*std_auth01W7
  wstd_auth01W10 =~ 1*std_auth01W10
  wstd_auth01W11 =~ 1*std_auth01W11 
  wstd_auth01W14 =~ 1*std_auth01W14

  wstd_redist_prefW7 =~ 1*std_redistSelf01W7
  wstd_redist_prefW10 =~ 1*std_redistSelf01W10
  wstd_redist_prefW11 =~ 1*std_redistSelf01W11
  wstd_redist_prefW14 =~ 1*std_redistSelf01W14

  # Estimate the lagged effects between the within-person centered variables.
  wstd_auth01W10 ~ c(a1, a1)*wstd_auth01W7 + c(b1, b1)*wstd_redist_prefW7 
  wstd_redist_prefW10 ~ c(c1, c1)*wstd_auth01W7 + c(d1, d1)*wstd_redist_prefW7
  wstd_auth01W11 ~ c(a2, a2)*wstd_auth01W10 + c(b2, b2)*wstd_redist_prefW10
  wstd_redist_prefW11 ~ c(c2, c2)*wstd_auth01W10 + c(d2, d2)*wstd_redist_prefW10
  wstd_auth01W14 ~ c(a3, a3)*wstd_auth01W11 + c(b3, b3)*wstd_redist_prefW11
  wstd_redist_prefW14 ~ c(c3, c3)*wstd_auth01W11 + c(d3, d3)*wstd_redist_prefW11

  # Estimate the covariance between the within-person centered variables at the first wave. 
  wstd_auth01W7 ~~ wstd_redist_prefW7 # Covariance
  
  # Estimate the covariances between the residuals of the within-person centered variables (the innovations).
  wstd_auth01W10 ~~ wstd_redist_prefW10
  wstd_auth01W11 ~~ wstd_redist_prefW11
  wstd_auth01W14 ~~ wstd_redist_prefW14
  
  
  # Estimate the variance and covariance of the random intercepts. 
  RIstd_auth01W ~~ RIstd_auth01W
  RIstd_redist_prefW ~~ RIstd_redist_prefW
  RIstd_auth01W ~~ RIstd_redist_prefW

  # Estimate the (residual) variance of the within-person centered variables.
  wstd_auth01W7 ~~ wstd_auth01W7 # Variances
  wstd_redist_prefW7 ~~ wstd_redist_prefW7 
  wstd_auth01W10 ~~ wstd_auth01W10 # Residual variances
  wstd_redist_prefW10 ~~ wstd_redist_prefW10 
  wstd_auth01W11 ~~ wstd_auth01W11
  wstd_redist_prefW11 ~~ wstd_redist_prefW11 
  wstd_auth01W14 ~~ wstd_auth01W14 
  wstd_redist_prefW14 ~~ wstd_redist_prefW14 
  
  # Fix error variance of indicators to (1 - alpha)*var
std_auth01W10 ~~ 0.0058*std_auth01W10
std_auth01W11 ~~ 0.002*std_auth01W11
std_auth01W14 ~~ 0.00725*std_auth01W14
std_redistSelf01W10 ~~ 0.01038*std_redistSelf01W10
std_redistSelf01W11 ~~ 0.00933*std_redistSelf01W11
std_redistSelf01W14 ~~ 0*std_redistSelf01W14

'

std_redist_attC.fit <- lavaan(std_redist_attC, 
                              data = att_dat,
                              group = "polatt_group",
                              estimator = "MLR",
                              missing = 'ML', 
                              meanstructure = T, 
                              int.ov.free = T) 

anova(std_redist_attU.fit, std_redist_attC.fit)

## Immigration

std_immig_attU <- '
 # Create between components (random intercepts)
  RIstd_auth01W  =~ 1*std_auth01W7 + 1*std_auth01W10 + 1*std_auth01W11 + 1*std_auth01W14 
  RIim_prefW =~ 1*std_im_pref_2W7 + 1*std_im_pref_2W10 + 1*std_im_pref_2W11 + 1*std_im_pref_2W14 
  
  # Create within-person centered variables
  wstd_auth01W7 =~ 1*std_auth01W7
  wstd_auth01W10 =~ 1*std_auth01W10
  wstd_auth01W11 =~ 1*std_auth01W11 
  wstd_auth01W14 =~ 1*std_auth01W14

  wim_prefW7 =~ 1*std_im_pref_2W7
  wim_prefW10 =~ 1*std_im_pref_2W10
  wim_prefW11 =~ 1*std_im_pref_2W11
  wim_prefW14 =~ 1*std_im_pref_2W14

  # Estimate the lagged effects between the within-person centered variables.
  wstd_auth01W10 + wim_prefW10 ~ wstd_auth01W7 + wim_prefW7
  wstd_auth01W11 + wim_prefW11 ~ wstd_auth01W10 + wim_prefW10
  wstd_auth01W14 + wim_prefW14 ~ wstd_auth01W11 + wim_prefW11

  # Estimate the covariance between the within-person centered variables at the first wave. 
  wstd_auth01W7 ~~ wim_prefW7 # Covariance
  
  # Estimate the covariances between the residuals of the within-person centered variables (the innovations).
  wstd_auth01W10 ~~ wim_prefW10
  wstd_auth01W11 ~~ wim_prefW11
  wstd_auth01W14 ~~ wim_prefW14
  
  
  # Estimate the variance and covariance of the random intercepts. 
  RIstd_auth01W ~~ RIstd_auth01W
  RIim_prefW ~~ RIim_prefW
  RIstd_auth01W ~~ RIim_prefW

  # Estimate the (residual) variance of the within-person centered variables.
  wstd_auth01W7 ~~ wstd_auth01W7 # Variances
  wim_prefW7 ~~ wim_prefW7 
  wstd_auth01W10 ~~ wstd_auth01W10 # Residual variances
  wim_prefW10 ~~ wim_prefW10 
  wstd_auth01W11 ~~ wstd_auth01W11
  wim_prefW11 ~~ wim_prefW11 
  wstd_auth01W14 ~~ wstd_auth01W14 
  wim_prefW14 ~~ wim_prefW14 
  
  # Fix error variance of indicators to (1 - alpha)*var
std_auth01W7 ~~ 0.02*std_auth01W7
std_auth01W10 ~~ 0.016*std_auth01W10
std_auth01W11 ~~ 0.018*std_auth01W11
std_auth01W14 ~~ 0.019*std_auth01W14
std_im_pref_2W7 ~~ 0.0147*std_im_pref_2W7
std_im_pref_2W10 ~~ 0.000155*std_im_pref_2W10
std_im_pref_2W11 ~~ 0.000147*std_im_pref_2W11
std_im_pref_2W14 ~~ 0.000512*std_im_pref_2W14

  '
std_immig_attU.fit <- lavaan(std_immig_attU, 
                               data = att_dat, 
                               group = "polatt_group",
                               estimator = "MLR",
                               missing = 'ML',
                               meanstructure = T, 
                               int.ov.free = T) 
summary(std_immig_attU.fit, standardized = T)

std_immig_attC <- '
 # Create between components (random intercepts)
  RIstd_auth01W  =~ 1*std_auth01W7 + 1*std_auth01W10 + 1*std_auth01W11 + 1*std_auth01W14 
  RIim_prefW =~ 1*std_im_pref_2W7 + 1*std_im_pref_2W10 + 1*std_im_pref_2W11 + 1*std_im_pref_2W14 
  
  # Create within-person centered variables
  wstd_auth01W7 =~ 1*std_auth01W7
  wstd_auth01W10 =~ 1*std_auth01W10
  wstd_auth01W11 =~ 1*std_auth01W11 
  wstd_auth01W14 =~ 1*std_auth01W14

  wim_prefW7 =~ 1*std_im_pref_2W7
  wim_prefW10 =~ 1*std_im_pref_2W10
  wim_prefW11 =~ 1*std_im_pref_2W11
  wim_prefW14 =~ 1*std_im_pref_2W14

  # Estimate the lagged effects between the within-person centered variables.
  wstd_auth01W10 ~ c(a1, a1)*wstd_auth01W7 + c(b1, b1)*wim_prefW7
  wim_prefW10 ~ c(c1, c1)*wstd_auth01W7 + c(d1, d1)*wim_prefW7
  wstd_auth01W11 ~ c(a2, a2)*wstd_auth01W10 + c(b2, b2)*wim_prefW10
  wim_prefW11 ~ c(c2, c2)*wstd_auth01W10 + c(d2, d2)*wim_prefW10
  wstd_auth01W14 ~ c(a3, a3)*wstd_auth01W11 + c(b3, b3)*wim_prefW11
  wim_prefW14 ~ c(c3, c3)*wstd_auth01W11 + c(d3, d3)*wim_prefW11

  # Estimate the covariance between the within-person centered variables at the first wave. 
  wstd_auth01W7 ~~ wim_prefW7 # Covariance
  
  # Estimate the covariances between the residuals of the within-person centered variables (the innovations).
  wstd_auth01W10 ~~ wim_prefW10
  wstd_auth01W11 ~~ wim_prefW11
  wstd_auth01W14 ~~ wim_prefW14
  
  
  # Estimate the variance and covariance of the random intercepts. 
  RIstd_auth01W ~~ RIstd_auth01W
  RIim_prefW ~~ RIim_prefW
  RIstd_auth01W ~~ RIim_prefW

  # Estimate the (residual) variance of the within-person centered variables.
  wstd_auth01W7 ~~ wstd_auth01W7 # Variances
  wim_prefW7 ~~ wim_prefW7 
  wstd_auth01W10 ~~ wstd_auth01W10 # Residual variances
  wim_prefW10 ~~ wim_prefW10 
  wstd_auth01W11 ~~ wstd_auth01W11
  wim_prefW11 ~~ wim_prefW11 
  wstd_auth01W14 ~~ wstd_auth01W14 
  wim_prefW14 ~~ wim_prefW14 
  
  # Fix error variance of indicators to (1 - alpha)*var
std_auth01W7 ~~ 0.02*std_auth01W7
std_auth01W10 ~~ 0.016*std_auth01W10
std_auth01W11 ~~ 0.018*std_auth01W11
std_auth01W14 ~~ 0.019*std_auth01W14
std_im_pref_2W7 ~~ 0.0147*std_im_pref_2W7
std_im_pref_2W10 ~~ 0.000155*std_im_pref_2W10
std_im_pref_2W11 ~~ 0.000147*std_im_pref_2W11
std_im_pref_2W14 ~~ 0.000512*std_im_pref_2W14

  '
std_immig_attC.fit <- lavaan(std_immig_attC, 
                               data = att_dat, 
                               group = "polatt_group",
                               estimator = "MLR",
                               missing = 'ML',
                               meanstructure = T, 
                               int.ov.free = T) 
summary(std_immig_attC.fit, standardized = T)

anova(std_immig_attU.fit, std_immig_attC.fit)

## EU integration

std_eu_attU <- '
  # Create between components (random intercepts)
  RIstd_auth01W  =~ 1*std_auth01W7 + 1*std_auth01W10 + 1*std_auth01W11 + 1*std_auth01W14 
  RIstd_eu_intW =~ 1*std_eu_intW7 + 1*std_eu_intW10 + 1*std_eu_intW11 + 1*std_eu_intW14 
  
  # Create within-person centered variables
  wstd_auth01W7 =~ 1*std_auth01W7
  wstd_auth01W10 =~ 1*std_auth01W10
  wstd_auth01W11 =~ 1*std_auth01W11 
  wstd_auth01W14 =~ 1*std_auth01W14

  wstd_eu_intW7 =~ 1*std_eu_intW7
  wstd_eu_intW10 =~ 1*std_eu_intW10
  wstd_eu_intW11 =~ 1*std_eu_intW11
  wstd_eu_intW14 =~ 1*std_eu_intW14

  # Estimate the lagged effects between the within-person centered variables.
  wstd_auth01W10 + wstd_eu_intW10 ~ wstd_auth01W7 + wstd_eu_intW7
  wstd_auth01W11 + wstd_eu_intW11 ~ wstd_auth01W10 + wstd_eu_intW10
  wstd_auth01W14 + wstd_eu_intW14 ~ wstd_auth01W11 + wstd_eu_intW11

  # Estimate the covariance between the within-person centered variables at the first wave. 
  wstd_auth01W7 ~~ wstd_eu_intW7 # Covariance
  
  # Estimate the covariances between the residuals of the within-person centered variables (the innovations).
  wstd_auth01W10 ~~ wstd_eu_intW10
  wstd_auth01W11 ~~ wstd_eu_intW11
  wstd_auth01W14 ~~ wstd_eu_intW14
  
  
  # Estimate the variance and covariance of the random intercepts. 
  RIstd_auth01W ~~ RIstd_auth01W
  RIstd_eu_intW ~~ RIstd_eu_intW
  RIstd_auth01W ~~ RIstd_eu_intW

  # Estimate the (residual) variance of the within-person centered variables.
  wstd_auth01W7 ~~ wstd_auth01W7 # Variances
  wstd_eu_intW7 ~~ wstd_eu_intW7 
  wstd_auth01W10 ~~ wstd_auth01W10 # Residual variances
  wstd_eu_intW10 ~~ wstd_eu_intW10 
  wstd_auth01W11 ~~ wstd_auth01W11
  wstd_eu_intW11 ~~ wstd_eu_intW11 
  wstd_auth01W14 ~~ wstd_auth01W14 
  wstd_eu_intW14 ~~ wstd_eu_intW14 
  
  # Fix error variance of indicators to (1 - alpha)*var
std_auth01W10 ~~ 0.0058*std_auth01W10
std_auth01W11 ~~ 0.002*std_auth01W11
std_auth01W14 ~~ 0.00725*std_auth01W14
std_eu_intW10 ~~ 0.0028*std_eu_intW10
std_eu_intW11 ~~ 0.00194*std_eu_intW11
std_eu_intW14 ~~ 0*std_eu_intW14


'

std_eu_attU.fit <- lavaan(std_eu_attU, 
                          estimator = "MLR",
                          data = att_dat,
                          group = "polatt_group",
                          missing = 'ML', 
                          meanstructure = T, 
                          int.ov.free = T) 
summary(std_eu_attU.fit, standardized = T)

std_eu_attC <- '
  # Create between components (random intercepts)
  RIstd_auth01W  =~ 1*std_auth01W7 + 1*std_auth01W10 + 1*std_auth01W11 + 1*std_auth01W14 
  RIstd_eu_intW =~ 1*std_eu_intW7 + 1*std_eu_intW10 + 1*std_eu_intW11 + 1*std_eu_intW14 
  
  # Create within-person centered variables
  wstd_auth01W7 =~ 1*std_auth01W7
  wstd_auth01W10 =~ 1*std_auth01W10
  wstd_auth01W11 =~ 1*std_auth01W11 
  wstd_auth01W14 =~ 1*std_auth01W14

  wstd_eu_intW7 =~ 1*std_eu_intW7
  wstd_eu_intW10 =~ 1*std_eu_intW10
  wstd_eu_intW11 =~ 1*std_eu_intW11
  wstd_eu_intW14 =~ 1*std_eu_intW14
  
  # Estimate the lagged effects between the within-person centered variables.
  wstd_auth01W10 ~ c(a1, a1)*wstd_auth01W7 + c(b1, b1)*wstd_eu_intW7
  wstd_eu_intW10 ~ c(c1, c1)*wstd_auth01W7 + c(d1, d1)*wstd_eu_intW7
  wstd_auth01W11 ~ c(a2, a2)*wstd_auth01W10 + c(b2, b2)*wstd_eu_intW10
  wstd_eu_intW11 ~ c(c2, c2)*wstd_auth01W10 + c(d2, d2)*wstd_eu_intW10
  wstd_auth01W14 ~ c(a3, a3)*wstd_auth01W11 + c(b3, b3)*wstd_eu_intW11
  wstd_eu_intW14 ~ c(c3, c3)*wstd_auth01W11 + c(d3, d3)*wstd_eu_intW11

  # Estimate the covariance between the within-person centered variables at the first wave. 
  wstd_auth01W7 ~~ wstd_eu_intW7 # Covariance
  
  # Estimate the covariances between the residuals of the within-person centered variables (the innovations).
  wstd_auth01W10 ~~ wstd_eu_intW10
  wstd_auth01W11 ~~ wstd_eu_intW11
  wstd_auth01W14 ~~ wstd_eu_intW14
  
  
  # Estimate the variance and covariance of the random intercepts. 
  RIstd_auth01W ~~ RIstd_auth01W
  RIstd_eu_intW ~~ RIstd_eu_intW
  RIstd_auth01W ~~ RIstd_eu_intW

  # Estimate the (residual) variance of the within-person centered variables.
  wstd_auth01W7 ~~ wstd_auth01W7 # Variances
  wstd_eu_intW7 ~~ wstd_eu_intW7 
  wstd_auth01W10 ~~ wstd_auth01W10 # Residual variances
  wstd_eu_intW10 ~~ wstd_eu_intW10 
  wstd_auth01W11 ~~ wstd_auth01W11
  wstd_eu_intW11 ~~ wstd_eu_intW11 
  wstd_auth01W14 ~~ wstd_auth01W14 
  wstd_eu_intW14 ~~ wstd_eu_intW14 
  
  # Fix error variance of indicators to (1 - alpha)*var
std_auth01W10 ~~ 0.0058*std_auth01W10
std_auth01W11 ~~ 0.002*std_auth01W11
std_auth01W14 ~~ 0.00725*std_auth01W14
std_eu_intW10 ~~ 0.0028*std_eu_intW10
std_eu_intW11 ~~ 0.00194*std_eu_intW11
std_eu_intW14 ~~ 0*std_eu_intW14
'

std_eu_attC.fit <- lavaan(std_eu_attC, 
                          estimator = "MLR",
                          data = att_dat,
                          group = "polatt_group",
                          missing = 'ML', 
                          meanstructure = T, 
                          int.ov.free = T) 
summary(std_eu_attC.fit, standardized = T)

anova(std_eu_attC.fit, std_eu_attU.fit)

#### Multiple-group CLPMs: Engagement - redistribution ####

####  Redistribution

std_redist_attU_CLPM <- '
  # Create between components (random intercepts)
  RIstd_auth01W  =~ 1*std_auth01W7 + 1*std_auth01W10 + 1*std_auth01W11 + 1*std_auth01W14 
  RIstd_redist_prefW =~ 1*std_redistSelf01W7 + 1*std_redistSelf01W10 + 1*std_redistSelf01W11 + 1*std_redistSelf01W14 
  
  # Create within-person centered variables
  wstd_auth01W7 =~ 1*std_auth01W7
  wstd_auth01W10 =~ 1*std_auth01W10
  wstd_auth01W11 =~ 1*std_auth01W11 
  wstd_auth01W14 =~ 1*std_auth01W14

  wstd_redist_prefW7 =~ 1*std_redistSelf01W7
  wstd_redist_prefW10 =~ 1*std_redistSelf01W10
  wstd_redist_prefW11 =~ 1*std_redistSelf01W11
  wstd_redist_prefW14 =~ 1*std_redistSelf01W14

  # Estimate the lagged effects between the within-person centered variables.
  wstd_auth01W10 + wstd_redist_prefW10 ~ wstd_auth01W7 + wstd_redist_prefW7
  wstd_auth01W11 + wstd_redist_prefW11 ~ wstd_auth01W10 + wstd_redist_prefW10
  wstd_auth01W14 + wstd_redist_prefW14 ~ wstd_auth01W11 + wstd_redist_prefW11

  # Estimate the covariance between the within-person centered variables at the first wave. 
  wstd_auth01W7 ~~ wstd_redist_prefW7 # Covariance
  
  # Estimate the covariances between the residuals of the within-person centered variables (the innovations).
  wstd_auth01W10 ~~ wstd_redist_prefW10
  wstd_auth01W11 ~~ wstd_redist_prefW11
  wstd_auth01W14 ~~ wstd_redist_prefW14
  
  
  # Constrain the variance and covariance of the random intercepts to zero 
  RIstd_auth01W ~~ 0*RIstd_auth01W
  RIstd_redist_prefW ~~ 0*RIstd_redist_prefW
  RIstd_auth01W ~~ 0*RIstd_redist_prefW

  # Estimate the (residual) variance of the within-person centered variables.
  wstd_auth01W7 ~~ wstd_auth01W7 # Variances
  wstd_redist_prefW7 ~~ wstd_redist_prefW7 
  wstd_auth01W10 ~~ wstd_auth01W10 # Residual variances
  wstd_redist_prefW10 ~~ wstd_redist_prefW10 
  wstd_auth01W11 ~~ wstd_auth01W11
  wstd_redist_prefW11 ~~ wstd_redist_prefW11 
  wstd_auth01W14 ~~ wstd_auth01W14 
  wstd_redist_prefW14 ~~ wstd_redist_prefW14 
  
  # Fix error variance of indicators to (1 - alpha)*var
std_auth01W10 ~~ 0.0058*std_auth01W10
std_auth01W11 ~~ 0.002*std_auth01W11
std_auth01W14 ~~ 0.00725*std_auth01W14
std_redistSelf01W10 ~~ 0.01038*std_redistSelf01W10
std_redistSelf01W11 ~~ 0.00933*std_redistSelf01W11
std_redistSelf01W14 ~~ 0*std_redistSelf01W14

'

std_redist_attU_CLPM.fit <- lavaan(std_redist_attU_CLPM, 
                                   data = att_dat,
                                   group = "polatt_group",
                                   estimator = "MLR",
                                   missing = 'ML', 
                                   meanstructure = T, 
                                   int.ov.free = T) 
summary(std_redist_attU_CLPM.fit, standardized = T)

std_redist_attC_CLPM <- '
  # Create between components (random intercepts)
  RIstd_auth01W  =~ 1*std_auth01W7 + 1*std_auth01W10 + 1*std_auth01W11 + 1*std_auth01W14 
  RIstd_redist_prefW =~ 1*std_redistSelf01W7 + 1*std_redistSelf01W10 + 1*std_redistSelf01W11 + 1*std_redistSelf01W14 
  
  # Create within-person centered variables
  wstd_auth01W7 =~ 1*std_auth01W7
  wstd_auth01W10 =~ 1*std_auth01W10
  wstd_auth01W11 =~ 1*std_auth01W11 
  wstd_auth01W14 =~ 1*std_auth01W14

  wstd_redist_prefW7 =~ 1*std_redistSelf01W7
  wstd_redist_prefW10 =~ 1*std_redistSelf01W10
  wstd_redist_prefW11 =~ 1*std_redistSelf01W11
  wstd_redist_prefW14 =~ 1*std_redistSelf01W14

  # Estimate the lagged effects between the within-person centered variables.
  wstd_auth01W10 ~ c(a1, a1)*wstd_auth01W7 + c(b1, b1)*wstd_redist_prefW7 
  wstd_redist_prefW10 ~ c(c1, c1)*wstd_auth01W7 + c(d1, d1)*wstd_redist_prefW7
  wstd_auth01W11 ~ c(a2, a2)*wstd_auth01W10 + c(b2, b2)*wstd_redist_prefW10
  wstd_redist_prefW11 ~ c(c2, c2)*wstd_auth01W10 + c(d2, d2)*wstd_redist_prefW10
  wstd_auth01W14 ~ c(a3, a3)*wstd_auth01W11 + c(b3, b3)*wstd_redist_prefW11
  wstd_redist_prefW14 ~ c(c3, c3)*wstd_auth01W11 + c(d3, d3)*wstd_redist_prefW11

  # Estimate the covariance between the within-person centered variables at the first wave. 
  wstd_auth01W7 ~~ wstd_redist_prefW7 # Covariance
  
  # Estimate the covariances between the residuals of the within-person centered variables (the innovations).
  wstd_auth01W10 ~~ wstd_redist_prefW10
  wstd_auth01W11 ~~ wstd_redist_prefW11
  wstd_auth01W14 ~~ wstd_redist_prefW14
  
  
  # Constrain the variance and covariance of the random intercepts to zero 
  RIstd_auth01W ~~ 0*RIstd_auth01W
  RIstd_redist_prefW ~~ 0*RIstd_redist_prefW
  RIstd_auth01W ~~ 0*RIstd_redist_prefW

  # Estimate the (residual) variance of the within-person centered variables.
  wstd_auth01W7 ~~ wstd_auth01W7 # Variances
  wstd_redist_prefW7 ~~ wstd_redist_prefW7 
  wstd_auth01W10 ~~ wstd_auth01W10 # Residual variances
  wstd_redist_prefW10 ~~ wstd_redist_prefW10 
  wstd_auth01W11 ~~ wstd_auth01W11
  wstd_redist_prefW11 ~~ wstd_redist_prefW11 
  wstd_auth01W14 ~~ wstd_auth01W14 
  wstd_redist_prefW14 ~~ wstd_redist_prefW14 
  
  # Fix error variance of indicators to (1 - alpha)*var
std_auth01W10 ~~ 0.0058*std_auth01W10
std_auth01W11 ~~ 0.002*std_auth01W11
std_auth01W14 ~~ 0.00725*std_auth01W14
std_redistSelf01W10 ~~ 0.01038*std_redistSelf01W10
std_redistSelf01W11 ~~ 0.00933*std_redistSelf01W11
std_redistSelf01W14 ~~ 0*std_redistSelf01W14

'

std_redist_attC_CLPM.fit <- lavaan(std_redist_attC_CLPM, 
                                   data = att_dat,
                                   group = "polatt_group",
                                   estimator = "MLR",
                                   missing = 'ML', 
                                   meanstructure = T, 
                                   int.ov.free = T) 


anova(std_redist_attU_CLPM.fit, std_redist_attC_CLPM.fit)

standardizedsolution(std_redist_attU_CLPM.fit)

std_redist_attU_CLPM_tvc <- '
  # Create between components (random intercepts)
  RIstd_auth01W  =~ 1*std_auth01W7 + 1*std_auth01W10 + 1*std_auth01W11 + 1*std_auth01W14 
  RIstd_redist_prefW =~ 1*std_redistSelf01W7 + 1*std_redistSelf01W10 + 1*std_redistSelf01W11 + 1*std_redistSelf01W14 
  
  # Create within-person centered variables
  wstd_auth01W7 =~ 1*std_auth01W7
  wstd_auth01W10 =~ 1*std_auth01W10
  wstd_auth01W11 =~ 1*std_auth01W11 
  wstd_auth01W14 =~ 1*std_auth01W14

  wstd_redist_prefW7 =~ 1*std_redistSelf01W7
  wstd_redist_prefW10 =~ 1*std_redistSelf01W10
  wstd_redist_prefW11 =~ 1*std_redistSelf01W11
  wstd_redist_prefW14 =~ 1*std_redistSelf01W14

  # Estimate the lagged effects between the within-person centered variables.
  wstd_auth01W10 + wstd_redist_prefW10 ~ wstd_auth01W7 + wstd_redist_prefW7
  wstd_auth01W11 + wstd_redist_prefW11 ~ wstd_auth01W10 + wstd_redist_prefW10
  wstd_auth01W14 + wstd_redist_prefW14 ~ wstd_auth01W11 + wstd_redist_prefW11

  # Estimate the covariance between the within-person centered variables at the first wave. 
  wstd_auth01W7 ~~ wstd_redist_prefW7 # Covariance
  
  # Estimate the covariances between the residuals of the within-person centered variables (the innovations).
  wstd_auth01W10 ~~ wstd_redist_prefW10
  wstd_auth01W11 ~~ wstd_redist_prefW11
  wstd_auth01W14 ~~ wstd_redist_prefW14
  
  
  # Constrain the variance and covariance of the random intercepts to zero 
  RIstd_auth01W ~~ 0*RIstd_auth01W
  RIstd_redist_prefW ~~ 0*RIstd_redist_prefW
  RIstd_auth01W ~~ 0*RIstd_redist_prefW

  # Estimate the (residual) variance of the within-person centered variables.
  wstd_auth01W7 ~~ wstd_auth01W7 # Variances
  wstd_redist_prefW7 ~~ wstd_redist_prefW7 
  wstd_auth01W10 ~~ wstd_auth01W10 # Residual variances
  wstd_redist_prefW10 ~~ wstd_redist_prefW10 
  wstd_auth01W11 ~~ wstd_auth01W11
  wstd_redist_prefW11 ~~ wstd_redist_prefW11 
  wstd_auth01W14 ~~ wstd_auth01W14 
  wstd_redist_prefW14 ~~ wstd_redist_prefW14 
  
  # Fix error variance of indicators to (1 - alpha)*var
std_auth01W10 ~~ 0.0058*std_auth01W10
std_auth01W11 ~~ 0.002*std_auth01W11
std_auth01W14 ~~ 0.00725*std_auth01W14
std_redistSelf01W10 ~~ 0.01038*std_redistSelf01W10
std_redistSelf01W11 ~~ 0.00933*std_redistSelf01W11
std_redistSelf01W14 ~~ 0*std_redistSelf01W14

# incorporate tvc - age
wstd_auth01W7 + wstd_redist_prefW7 ~ ageW7
wstd_auth01W10 + wstd_redist_prefW10 ~ ageW10
wstd_auth01W11 + wstd_redist_prefW11 ~ ageW11
wstd_auth01W14 + wstd_redist_prefW14 ~ ageW14

ageW7 ~~ ageW7
ageW10 ~~ ageW10
ageW11 ~~ ageW11
ageW14 ~~ ageW14

ageW7 ~~ ageW10 + ageW11 + ageW14
ageW10 ~~ ageW11 + ageW14
ageW11 ~~ ageW14

# incorporate tvc - ethnicity
wstd_auth01W7 + wstd_redist_prefW7 ~ ethnic7
wstd_auth01W10 + wstd_redist_prefW10 ~ ethnic10
wstd_auth01W11 + wstd_redist_prefW11 ~ ethnic11
wstd_auth01W14 + wstd_redist_prefW14 ~ ethnic14

ethnic7 ~~ ethnic7
ethnic10 ~~ ethnic10
ethnic11 ~~ ethnic11
ethnic14 ~~ ethnic14

ethnic7 ~~ ethnic10 + ethnic11 + ethnic14
ethnic10 ~~ ethnic11 + ethnic14
ethnic11 ~~ ethnic14

# incorporate tvc – social grade
wstd_auth01W7 + wstd_redist_prefW7 ~ p_socgradeW7
wstd_auth01W10 + wstd_redist_prefW10 ~ p_socgradeW10
wstd_auth01W11 + wstd_redist_prefW11 ~ p_socgradeW11
wstd_auth01W14 + wstd_redist_prefW14 ~ p_socgradeW14

p_socgradeW7 ~~ p_socgradeW7
p_socgradeW10 ~~ p_socgradeW10
p_socgradeW11 ~~ p_socgradeW11
p_socgradeW14 ~~ p_socgradeW14

p_socgradeW7 ~~ p_socgradeW10 + p_socgradeW11 + p_socgradeW14
p_socgradeW10 ~~ p_socgradeW11 + p_socgradeW14
p_socgradeW11 ~~ p_socgradeW14

# incorporate tvc – education
wstd_auth01W7 + wstd_redist_prefW7 ~ higheduc7
wstd_auth01W10 + wstd_redist_prefW10 ~ higheduc10
wstd_auth01W11 + wstd_redist_prefW11 ~ higheduc11
wstd_auth01W14 + wstd_redist_prefW14 ~ higheduc14

higheduc7 ~~ higheduc7
higheduc10 ~~ higheduc10
higheduc11 ~~ higheduc11
higheduc14 ~~ higheduc14

higheduc7 ~~ higheduc10 + higheduc11 + higheduc14
higheduc10 ~~ higheduc11 + higheduc14
higheduc11 ~~ higheduc14

# incorporate tvc – income
wstd_auth01W7 + wstd_redist_prefW7 ~ incomerec7
wstd_auth01W10 + wstd_redist_prefW10 ~ incomerec10
wstd_auth01W11 + wstd_redist_prefW11 ~ incomerec11
wstd_auth01W14 + wstd_redist_prefW14 ~ incomerec14

incomerec7 ~~ incomerec7
incomerec10 ~~ incomerec10
incomerec11 ~~ incomerec11
incomerec14 ~~ incomerec14

incomerec7 ~~ incomerec10 + incomerec11 + incomerec14
incomerec10 ~~ incomerec11 + incomerec14
incomerec11 ~~ incomerec14
'

std_redist_attU_CLPM_tvc.fit <- lavaan(std_redist_attU_CLPM_tvc, 
                                       data = att_dat,
                                       group = "polatt_group",
                                       estimator = "MLR",
                                       missing = 'ML', 
                                       meanstructure = T, 
                                       int.ov.free = T) 
summary(std_redist_attU_CLPM_tvc.fit, standardized = T)


### Equality constraints:

# High
std_redist_CLPM_H <- '
  # Create between components (random intercepts)
  RIstd_auth01W  =~ 1*std_auth01W7 + 1*std_auth01W10 + 1*std_auth01W11 + 1*std_auth01W14 
  RIstd_redist_prefW =~ 1*std_redistSelf01W7 + 1*std_redistSelf01W10 + 1*std_redistSelf01W11 + 1*std_redistSelf01W14 
  
  # Create within-person centered variables
  wstd_auth01W7 =~ NA*std_auth01W7
  wstd_auth01W10 =~ NA*std_auth01W10
  wstd_auth01W11 =~ NA*std_auth01W11 
  wstd_auth01W14 =~ NA*std_auth01W14

  wstd_redist_prefW7 =~ NA*std_redistSelf01W7
  wstd_redist_prefW10 =~ NA*std_redistSelf01W10
  wstd_redist_prefW11 =~ NA*std_redistSelf01W11
  wstd_redist_prefW14 =~ NA*std_redistSelf01W14

  # Estimate the lagged effects between the within-person centered variables.
  wstd_auth01W10 ~ a*wstd_auth01W7 + b*wstd_redist_prefW7
  wstd_redist_prefW10 ~ c*wstd_auth01W7 + d*wstd_redist_prefW7
  wstd_auth01W11 ~ a*wstd_auth01W10 + b*wstd_redist_prefW10
  wstd_redist_prefW11 ~ c*wstd_auth01W10 + d*wstd_redist_prefW10
  wstd_auth01W14 ~ a*wstd_auth01W11 + b*wstd_redist_prefW11
  wstd_redist_prefW14 ~ c*wstd_auth01W11 + d*wstd_redist_prefW11

  # Estimate the covariance between the within-person centered variables at the first wave. 
  wstd_auth01W7 ~~ cor1*wstd_redist_prefW7 # Covariance
  
  # Estimate the covariances between the residuals of the within-person centered variables (the innovations).
  wstd_auth01W10 ~~ rcov2*wstd_redist_prefW10
  wstd_auth01W11 ~~ rcov3*wstd_redist_prefW11
  wstd_auth01W14 ~~ rcov4*wstd_redist_prefW14
  
  
  # Estimate the variance and covariance of the random intercepts. 
  RIstd_auth01W ~~ 0*RIstd_auth01W
  RIstd_redist_prefW ~~ 0*RIstd_redist_prefW
  RIstd_auth01W ~~ 0*RIstd_redist_prefW

  # Set variances of within-components at first wave to 1
  wstd_auth01W7 ~~ 1*wstd_auth01W7 # Variances
  wstd_redist_prefW7 ~~ 1*wstd_redist_prefW7 
  
  wstd_auth01W10 ~~ rvx2*wstd_auth01W10 # Residual variances
  wstd_redist_prefW10 ~~ rvy2*wstd_redist_prefW10 
  wstd_auth01W11 ~~ rvx3*wstd_auth01W11
  wstd_redist_prefW11 ~~ rvy3*wstd_redist_prefW11 
  wstd_auth01W14 ~~ rvx4*wstd_auth01W14 
  wstd_redist_prefW14 ~~ rvy4*wstd_redist_prefW14 
  
  # Fix error variance of indicators to (1 - alpha)*var
std_auth01W10 ~~ 0.0058*std_auth01W10
std_auth01W11 ~~ 0.002*std_auth01W11
std_auth01W14 ~~ 0.00725*std_auth01W14
std_redistSelf01W10 ~~ 0.01038*std_redistSelf01W10
std_redistSelf01W11 ~~ 0.00933*std_redistSelf01W11
std_redistSelf01W14 ~~ 0*std_redistSelf01W14

 # Compute correlations of within-components at each wave
  cor2 := a*c + b*d + a*d*cor1 + b*c*cor1 + rcov2
  cor3 := a*c + b*d + a*d*cor2 + b*c*cor2 + rcov3
  cor4 := a*c + b*d + a*d*cor3 + b*c*cor3 + rcov4
 
  # Contrain residual variances of within-components such that variance of each 
  # within-component equals 1
  rvx2 == 1 - (a*a + b*b + 2*a*b*cor1)
  rvy2 == 1 - (c*c + d*d + 2*c*d*cor1)
  rvx3 == 1 - (a*a + b*b + 2*a*b*cor2)
  rvy3 == 1 - (c*c + d*d + 2*c*d*cor2)
  rvx4 == 1 - (a*a + b*b + 2*a*b*cor3)
  rvy4 == 1 - (c*c + d*d + 2*c*d*cor3)

'

std_redist_CLPM_H.fit <- lavaan(std_redist_CLPM_H, 
                                data = att_dat_high, 
                                estimator = "MLR",
                                missing = 'ML', meanstructure = T, int.ov.free = T) 
summary(std_redist_CLPM_H.fit, standardized = T)

# Low 
std_redist_CLPM_L <- '
  # Create between components (random intercepts)
  RIstd_auth01W  =~ 1*std_auth01W7 + 1*std_auth01W10 + 1*std_auth01W11 + 1*std_auth01W14 
  RIstd_redist_prefW =~ 1*std_redistSelf01W7 + 1*std_redistSelf01W10 + 1*std_redistSelf01W11 + 1*std_redistSelf01W14 
  
  # Create within-person centered variables
  wstd_auth01W7 =~ NA*std_auth01W7
  wstd_auth01W10 =~ NA*std_auth01W10
  wstd_auth01W11 =~ NA*std_auth01W11 
  wstd_auth01W14 =~ NA*std_auth01W14

  wstd_redist_prefW7 =~ NA*std_redistSelf01W7
  wstd_redist_prefW10 =~ NA*std_redistSelf01W10
  wstd_redist_prefW11 =~ NA*std_redistSelf01W11
  wstd_redist_prefW14 =~ NA*std_redistSelf01W14

  # Estimate the lagged effects between the within-person centered variables.
  wstd_auth01W10 ~ a*wstd_auth01W7 + b*wstd_redist_prefW7
  wstd_redist_prefW10 ~ c*wstd_auth01W7 + d*wstd_redist_prefW7
  wstd_auth01W11 ~ a*wstd_auth01W10 + b*wstd_redist_prefW10
  wstd_redist_prefW11 ~ c*wstd_auth01W10 + d*wstd_redist_prefW10
  wstd_auth01W14 ~ a*wstd_auth01W11 + b*wstd_redist_prefW11
  wstd_redist_prefW14 ~ c*wstd_auth01W11 + d*wstd_redist_prefW11

  # Estimate the covariance between the within-person centered variables at the first wave. 
  wstd_auth01W7 ~~ cor1*wstd_redist_prefW7 # Covariance
  
  # Estimate the covariances between the residuals of the within-person centered variables (the innovations).
  wstd_auth01W10 ~~ rcov2*wstd_redist_prefW10
  wstd_auth01W11 ~~ rcov3*wstd_redist_prefW11
  wstd_auth01W14 ~~ rcov4*wstd_redist_prefW14
  
  
  # Estimate the variance and covariance of the random intercepts. 
  RIstd_auth01W ~~ 0*RIstd_auth01W
  RIstd_redist_prefW ~~ 0*RIstd_redist_prefW
  RIstd_auth01W ~~ 0*RIstd_redist_prefW

  # Set variances of within-components at first wave to 1
  wstd_auth01W7 ~~ 1*wstd_auth01W7 # Variances
  wstd_redist_prefW7 ~~ 1*wstd_redist_prefW7 
  
  wstd_auth01W10 ~~ rvx2*wstd_auth01W10 # Residual variances
  wstd_redist_prefW10 ~~ rvy2*wstd_redist_prefW10 
  wstd_auth01W11 ~~ rvx3*wstd_auth01W11
  wstd_redist_prefW11 ~~ rvy3*wstd_redist_prefW11 
  wstd_auth01W14 ~~ rvx4*wstd_auth01W14 
  wstd_redist_prefW14 ~~ rvy4*wstd_redist_prefW14 
  
  # Fix error variance of indicators to (1 - alpha)*var
std_auth01W10 ~~ 0.0058*std_auth01W10
std_auth01W11 ~~ 0.002*std_auth01W11
std_auth01W14 ~~ 0.00725*std_auth01W14
std_redistSelf01W10 ~~ 0.01038*std_redistSelf01W10
std_redistSelf01W11 ~~ 0.00933*std_redistSelf01W11
std_redistSelf01W14 ~~ 0*std_redistSelf01W14

 # Compute correlations of within-components at each wave
  cor2 := a*c + b*d + a*d*cor1 + b*c*cor1 + rcov2
  cor3 := a*c + b*d + a*d*cor2 + b*c*cor2 + rcov3
  cor4 := a*c + b*d + a*d*cor3 + b*c*cor3 + rcov4
 
  # Contrain residual variances of within-components such that variance of each 
  # within-component equals 1
  rvx2 == 1 - (a*a + b*b + 2*a*b*cor1)
  rvy2 == 1 - (c*c + d*d + 2*c*d*cor1)
  rvx3 == 1 - (a*a + b*b + 2*a*b*cor2)
  rvy3 == 1 - (c*c + d*d + 2*c*d*cor2)
  rvx4 == 1 - (a*a + b*b + 2*a*b*cor3)
  rvy4 == 1 - (c*c + d*d + 2*c*d*cor3)

'

std_redist_CLPM_L.fit <- lavaan(std_redist_CLPM_L, data = att_dat_low, 
                                estimator = "MLR",
                                missing = 'ML', 
                                meanstructure = T, 
                                int.ov.free = T) 
summary(std_redist_CLPM_L.fit, standardized = T)

## Model fit

std_redist_CLPM_H_noEQ <- '
  # Create between components (random intercepts)
  RIstd_auth01W  =~ 1*std_auth01W7 + 1*std_auth01W10 + 1*std_auth01W11 + 1*std_auth01W14 
  RIstd_redist_prefW =~ 1*std_redistSelf01W7 + 1*std_redistSelf01W10 + 1*std_redistSelf01W11 + 1*std_redistSelf01W14 
  
  # Create within-person centered variables
  wstd_auth01W7 =~ 1*std_auth01W7
  wstd_auth01W10 =~ 1*std_auth01W10
  wstd_auth01W11 =~ 1*std_auth01W11 
  wstd_auth01W14 =~ 1*std_auth01W14

  wstd_redist_prefW7 =~ 1*std_redistSelf01W7
  wstd_redist_prefW10 =~ 1*std_redistSelf01W10
  wstd_redist_prefW11 =~ 1*std_redistSelf01W11
  wstd_redist_prefW14 =~ 1*std_redistSelf01W14

  # Estimate the lagged effects between the within-person centered variables.
  wstd_auth01W10 + wstd_redist_prefW10 ~ wstd_auth01W7 + wstd_redist_prefW7
  wstd_auth01W11 + wstd_redist_prefW11 ~ wstd_auth01W10 + wstd_redist_prefW10
  wstd_auth01W14 + wstd_redist_prefW14 ~ wstd_auth01W11 + wstd_redist_prefW11

  # Estimate the covariance between the within-person centered variables at the first wave. 
  wstd_auth01W7 ~~ wstd_redist_prefW7 # Covariance
  
  # Estimate the covariances between the residuals of the within-person centered variables (the innovations).
  wstd_auth01W10 ~~ wstd_redist_prefW10
  wstd_auth01W11 ~~ wstd_redist_prefW11
  wstd_auth01W14 ~~ wstd_redist_prefW14
  
  
  # Constrain the variance and covariance of the random intercepts to zero  
  RIstd_auth01W ~~ 0*RIstd_auth01W
  RIstd_redist_prefW ~~ 0*RIstd_redist_prefW
  RIstd_auth01W ~~ 0*RIstd_redist_prefW

  # Estimate the (residual) variance of the within-person centered variables.
  wstd_auth01W7 ~~ wstd_auth01W7 # Variances
  wstd_redist_prefW7 ~~ wstd_redist_prefW7 
  wstd_auth01W10 ~~ wstd_auth01W10 # Residual variances
  wstd_redist_prefW10 ~~ wstd_redist_prefW10 
  wstd_auth01W11 ~~ wstd_auth01W11
  wstd_redist_prefW11 ~~ wstd_redist_prefW11 
  wstd_auth01W14 ~~ wstd_auth01W14 
  wstd_redist_prefW14 ~~ wstd_redist_prefW14 
  
  # Fix error variance of indicators to (1 - alpha)*var
std_auth01W10 ~~ 0.0058*std_auth01W10
std_auth01W11 ~~ 0.002*std_auth01W11
std_auth01W14 ~~ 0.00725*std_auth01W14
std_redistSelf01W10 ~~ 0.01038*std_redistSelf01W10
std_redistSelf01W11 ~~ 0.00933*std_redistSelf01W11
std_redistSelf01W14 ~~ 0*std_redistSelf01W14

'

std_redist_CLPM_H_noEQ.fit <- lavaan(std_redist_CLPM_H_noEQ, 
                                     data = att_dat_high, 
                                     estimator = "MLR",
                                     missing = 'ML', meanstructure = T, 
                                     int.ov.free = T) 
summary(std_redist_CLPM_H_noEQ.fit)

std_redist_CLPM_L_noEQ <- '
  # Create between components (random intercepts)
  RIstd_auth01W  =~ 1*std_auth01W7 + 1*std_auth01W10 + 1*std_auth01W11 + 1*std_auth01W14 
  RIstd_redist_prefW =~ 1*std_redistSelf01W7 + 1*std_redistSelf01W10 + 1*std_redistSelf01W11 + 1*std_redistSelf01W14 
  
  # Create within-person centered variables
  wstd_auth01W7 =~ 1*std_auth01W7
  wstd_auth01W10 =~ 1*std_auth01W10
  wstd_auth01W11 =~ 1*std_auth01W11 
  wstd_auth01W14 =~ 1*std_auth01W14

  wstd_redist_prefW7 =~ 1*std_redistSelf01W7
  wstd_redist_prefW10 =~ 1*std_redistSelf01W10
  wstd_redist_prefW11 =~ 1*std_redistSelf01W11
  wstd_redist_prefW14 =~ 1*std_redistSelf01W14

  # Estimate the lagged effects between the within-person centered variables.
  wstd_auth01W10 + wstd_redist_prefW10 ~ wstd_auth01W7 + wstd_redist_prefW7
  wstd_auth01W11 + wstd_redist_prefW11 ~ wstd_auth01W10 + wstd_redist_prefW10
  wstd_auth01W14 + wstd_redist_prefW14 ~ wstd_auth01W11 + wstd_redist_prefW11

  # Estimate the covariance between the within-person centered variables at the first wave. 
  wstd_auth01W7 ~~ wstd_redist_prefW7 # Covariance
  
  # Estimate the covariances between the residuals of the within-person centered variables (the innovations).
  wstd_auth01W10 ~~ wstd_redist_prefW10
  wstd_auth01W11 ~~ wstd_redist_prefW11
  wstd_auth01W14 ~~ wstd_redist_prefW14
  
  
  # Constrain the variance and covariance of the random intercepts to zero  
  RIstd_auth01W ~~ 0*RIstd_auth01W
  RIstd_redist_prefW ~~ 0*RIstd_redist_prefW
  RIstd_auth01W ~~ 0*RIstd_redist_prefW

  # Estimate the (residual) variance of the within-person centered variables.
  wstd_auth01W7 ~~ wstd_auth01W7 # Variances
  wstd_redist_prefW7 ~~ wstd_redist_prefW7 
  wstd_auth01W10 ~~ wstd_auth01W10 # Residual variances
  wstd_redist_prefW10 ~~ wstd_redist_prefW10 
  wstd_auth01W11 ~~ wstd_auth01W11
  wstd_redist_prefW11 ~~ wstd_redist_prefW11 
  wstd_auth01W14 ~~ wstd_auth01W14 
  wstd_redist_prefW14 ~~ wstd_redist_prefW14 
  
  # Fix error variance of indicators to (1 - alpha)*var
std_auth01W10 ~~ 0.0058*std_auth01W10
std_auth01W11 ~~ 0.002*std_auth01W11
std_auth01W14 ~~ 0.00725*std_auth01W14
std_redistSelf01W10 ~~ 0.01038*std_redistSelf01W10
std_redistSelf01W11 ~~ 0.00933*std_redistSelf01W11
std_redistSelf01W14 ~~ 0*std_redistSelf01W14

'

std_redist_CLPM_L_noEQ.fit <- lavaan(std_redist_CLPM_L_noEQ, 
                                     data = att_dat_low, 
                                     estimator = "MLR",
                                     missing = 'ML', meanstructure = T, 
                                     int.ov.free = T) 
summary(std_redist_CLPM_L_noEQ.fit)


fitmeasures(std_redist_CLPM_H_noEQ.fit)
fitmeasures(std_redist_CLPM_H.fit)

fitmeasures(std_redist_CLPM_L_noEQ.fit)
fitmeasures(std_redist_CLPM_L.fit)

summary(std_redist_CLPM_H.fit, standardized = T)
summary(std_redist_CLPM_L.fit, standardized = T)

standardizedsolution(std_redist_CLPM_H.fit)
standardizedsolution(std_redist_CLPM_L.fit)
standardizedsolution(std_redist_CLPM_L_noEQ.fit)
standardizedsolution(std_redist_CLPM_H_noEQ.fit)

## Covariate models 

# High
std_redist_CLPM_H_cov <- '
  # Create between components (random intercepts)
  RIstd_auth01W  =~ 1*std_auth01W7 + 1*std_auth01W10 + 1*std_auth01W11 + 1*std_auth01W14 
  RIstd_redist_prefW =~ 1*std_redistSelf01W7 + 1*std_redistSelf01W10 + 1*std_redistSelf01W11 + 1*std_redistSelf01W14 
  
  # Create within-person centered variables
  wstd_auth01W7 =~ NA*std_auth01W7
  wstd_auth01W10 =~ NA*std_auth01W10
  wstd_auth01W11 =~ NA*std_auth01W11 
  wstd_auth01W14 =~ NA*std_auth01W14

  wstd_redist_prefW7 =~ NA*std_redistSelf01W7
  wstd_redist_prefW10 =~ NA*std_redistSelf01W10
  wstd_redist_prefW11 =~ NA*std_redistSelf01W11
  wstd_redist_prefW14 =~ NA*std_redistSelf01W14

  # Estimate the lagged effects between the within-person centered variables.
  wstd_auth01W10 ~ a*wstd_auth01W7 + b*wstd_redist_prefW7
  wstd_redist_prefW10 ~ c*wstd_auth01W7 + d*wstd_redist_prefW7
  wstd_auth01W11 ~ a*wstd_auth01W10 + b*wstd_redist_prefW10
  wstd_redist_prefW11 ~ c*wstd_auth01W10 + d*wstd_redist_prefW10
  wstd_auth01W14 ~ a*wstd_auth01W11 + b*wstd_redist_prefW11
  wstd_redist_prefW14 ~ c*wstd_auth01W11 + d*wstd_redist_prefW11

  # Estimate the covariance between the within-person centered variables at the first wave. 
  wstd_auth01W7 ~~ cor1*wstd_redist_prefW7 # Covariance
  
  # Estimate the covariances between the residuals of the within-person centered variables (the innovations).
  wstd_auth01W10 ~~ rcov2*wstd_redist_prefW10
  wstd_auth01W11 ~~ rcov3*wstd_redist_prefW11
  wstd_auth01W14 ~~ rcov4*wstd_redist_prefW14
  
  
  # Estimate the variance and covariance of the random intercepts. 
  RIstd_auth01W ~~ 0*RIstd_auth01W
  RIstd_redist_prefW ~~ 0*RIstd_redist_prefW
  RIstd_auth01W ~~ 0*RIstd_redist_prefW

  # Set variances of within-components at first wave to 1
  wstd_auth01W7 ~~ 1*wstd_auth01W7 # Variances
  wstd_redist_prefW7 ~~ 1*wstd_redist_prefW7 
  
  wstd_auth01W10 ~~ rvx2*wstd_auth01W10 # Residual variances
  wstd_redist_prefW10 ~~ rvy2*wstd_redist_prefW10 
  wstd_auth01W11 ~~ rvx3*wstd_auth01W11
  wstd_redist_prefW11 ~~ rvy3*wstd_redist_prefW11 
  wstd_auth01W14 ~~ rvx4*wstd_auth01W14 
  wstd_redist_prefW14 ~~ rvy4*wstd_redist_prefW14 
  
  # Fix error variance of indicators to (1 - alpha)*var
std_auth01W10 ~~ 0.0058*std_auth01W10
std_auth01W11 ~~ 0.002*std_auth01W11
std_auth01W14 ~~ 0.00725*std_auth01W14
std_redistSelf01W10 ~~ 0.01038*std_redistSelf01W10
std_redistSelf01W11 ~~ 0.00933*std_redistSelf01W11
std_redistSelf01W14 ~~ 0*std_redistSelf01W14

 # Compute correlations of within-components at each wave
  cor2 := a*c + b*d + a*d*cor1 + b*c*cor1 + rcov2
  cor3 := a*c + b*d + a*d*cor2 + b*c*cor2 + rcov3
  cor4 := a*c + b*d + a*d*cor3 + b*c*cor3 + rcov4
 
  # Contrain residual variances of within-components such that variance of each 
  # within-component equals 1
  rvx2 == 1 - (a*a + b*b + 2*a*b*cor1)
  rvy2 == 1 - (c*c + d*d + 2*c*d*cor1)
  rvx3 == 1 - (a*a + b*b + 2*a*b*cor2)
  rvy3 == 1 - (c*c + d*d + 2*c*d*cor2)
  rvx4 == 1 - (a*a + b*b + 2*a*b*cor3)
  rvy4 == 1 - (c*c + d*d + 2*c*d*cor3)

# incorporate tvc - age
wstd_auth01W7 + wstd_redist_prefW7 ~ ageW7
wstd_auth01W10 + wstd_redist_prefW10 ~ ageW10
wstd_auth01W11 + wstd_redist_prefW11 ~ ageW11
wstd_auth01W14 + wstd_redist_prefW14 ~ ageW14

ageW7 ~~ ageW7
ageW10 ~~ ageW10
ageW11 ~~ ageW11
ageW14 ~~ ageW14

ageW7 ~~ ageW10 + ageW11 + ageW14
ageW10 ~~ ageW11 + ageW14
ageW11 ~~ ageW14

# incorporate tvc - ethnicity
wstd_auth01W7 + wstd_redist_prefW7 ~ ethnic7
wstd_auth01W10 + wstd_redist_prefW10 ~ ethnic10
wstd_auth01W11 + wstd_redist_prefW11 ~ ethnic11
wstd_auth01W14 + wstd_redist_prefW14 ~ ethnic14

ethnic7 ~~ ethnic7
ethnic10 ~~ ethnic10
ethnic11 ~~ ethnic11
ethnic14 ~~ ethnic14

ethnic7 ~~ ethnic10 + ethnic11 + ethnic14
ethnic10 ~~ ethnic11 + ethnic14
ethnic11 ~~ ethnic14

# incorporate tvc – social grade
wstd_auth01W7 + wstd_redist_prefW7 ~ p_socgradeW7
wstd_auth01W10 + wstd_redist_prefW10 ~ p_socgradeW10
wstd_auth01W11 + wstd_redist_prefW11 ~ p_socgradeW11
wstd_auth01W14 + wstd_redist_prefW14 ~ p_socgradeW14

p_socgradeW7 ~~ p_socgradeW7
p_socgradeW10 ~~ p_socgradeW10
p_socgradeW11 ~~ p_socgradeW11
p_socgradeW14 ~~ p_socgradeW14

p_socgradeW7 ~~ p_socgradeW10 + p_socgradeW11 + p_socgradeW14
p_socgradeW10 ~~ p_socgradeW11 + p_socgradeW14
p_socgradeW11 ~~ p_socgradeW14

# incorporate tvc – education
wstd_auth01W7 + wstd_redist_prefW7 ~ higheduc7
wstd_auth01W10 + wstd_redist_prefW10 ~ higheduc10
wstd_auth01W11 + wstd_redist_prefW11 ~ higheduc11
wstd_auth01W14 + wstd_redist_prefW14 ~ higheduc14

higheduc7 ~~ higheduc7
higheduc10 ~~ higheduc10
higheduc11 ~~ higheduc11
higheduc14 ~~ higheduc14

higheduc7 ~~ higheduc10 + higheduc11 + higheduc14
higheduc10 ~~ higheduc11 + higheduc14
higheduc11 ~~ higheduc14

# incorporate tvc – income
wstd_auth01W7 + wstd_redist_prefW7 ~ incomerec7
wstd_auth01W10 + wstd_redist_prefW10 ~ incomerec10
wstd_auth01W11 + wstd_redist_prefW11 ~ incomerec11
wstd_auth01W14 + wstd_redist_prefW14 ~ incomerec14

incomerec7 ~~ incomerec7
incomerec10 ~~ incomerec10
incomerec11 ~~ incomerec11
incomerec14 ~~ incomerec14

incomerec7 ~~ incomerec10 + incomerec11 + incomerec14
incomerec10 ~~ incomerec11 + incomerec14
incomerec11 ~~ incomerec14

'

std_redist_CLPM_H_cov.fit <- lavaan(std_redist_CLPM_H_cov, 
                                data = att_dat_high, 
                                estimator = "MLR",
                                missing = 'ML', meanstructure = T, int.ov.free = T) 
summary(std_redist_CLPM_H_cov.fit, standardized = T)

std_redist_CLPM_H_cov_noEQ <- '
  # Create between components (random intercepts)
  RIstd_auth01W  =~ 1*std_auth01W7 + 1*std_auth01W10 + 1*std_auth01W11 + 1*std_auth01W14 
  RIstd_redist_prefW =~ 1*std_redistSelf01W7 + 1*std_redistSelf01W10 + 1*std_redistSelf01W11 + 1*std_redistSelf01W14 
  
  # Create within-person centered variables
  wstd_auth01W7 =~ 1*std_auth01W7
  wstd_auth01W10 =~ 1*std_auth01W10
  wstd_auth01W11 =~ 1*std_auth01W11 
  wstd_auth01W14 =~ 1*std_auth01W14

  wstd_redist_prefW7 =~ 1*std_redistSelf01W7
  wstd_redist_prefW10 =~ 1*std_redistSelf01W10
  wstd_redist_prefW11 =~ 1*std_redistSelf01W11
  wstd_redist_prefW14 =~ 1*std_redistSelf01W14

  # Estimate the lagged effects between the within-person centered variables.
  wstd_auth01W10 + wstd_redist_prefW10 ~ wstd_auth01W7 + wstd_redist_prefW7
  wstd_auth01W11 + wstd_redist_prefW11 ~ wstd_auth01W10 + wstd_redist_prefW10
  wstd_auth01W14 + wstd_redist_prefW14 ~ wstd_auth01W11 + wstd_redist_prefW11

  # Estimate the covariance between the within-person centered variables at the first wave. 
  wstd_auth01W7 ~~ wstd_redist_prefW7 # Covariance
  
  # Estimate the covariances between the residuals of the within-person centered variables (the innovations).
  wstd_auth01W10 ~~ wstd_redist_prefW10
  wstd_auth01W11 ~~ wstd_redist_prefW11
  wstd_auth01W14 ~~ wstd_redist_prefW14
  
  
  # Constrain the variance and covariance of the random intercepts to zero  
  RIstd_auth01W ~~ 0*RIstd_auth01W
  RIstd_redist_prefW ~~ 0*RIstd_redist_prefW
  RIstd_auth01W ~~ 0*RIstd_redist_prefW

  # Estimate the (residual) variance of the within-person centered variables.
  wstd_auth01W7 ~~ wstd_auth01W7 # Variances
  wstd_redist_prefW7 ~~ wstd_redist_prefW7 
  wstd_auth01W10 ~~ wstd_auth01W10 # Residual variances
  wstd_redist_prefW10 ~~ wstd_redist_prefW10 
  wstd_auth01W11 ~~ wstd_auth01W11
  wstd_redist_prefW11 ~~ wstd_redist_prefW11 
  wstd_auth01W14 ~~ wstd_auth01W14 
  wstd_redist_prefW14 ~~ wstd_redist_prefW14 
  
  # Fix error variance of indicators to (1 - alpha)*var
std_auth01W10 ~~ 0.0058*std_auth01W10
std_auth01W11 ~~ 0.002*std_auth01W11
std_auth01W14 ~~ 0.00725*std_auth01W14
std_redistSelf01W10 ~~ 0.01038*std_redistSelf01W10
std_redistSelf01W11 ~~ 0.00933*std_redistSelf01W11
std_redistSelf01W14 ~~ 0*std_redistSelf01W14

# incorporate tvc - age
wstd_auth01W7 + wstd_redist_prefW7 ~ ageW7
wstd_auth01W10 + wstd_redist_prefW10 ~ ageW10
wstd_auth01W11 + wstd_redist_prefW11 ~ ageW11
wstd_auth01W14 + wstd_redist_prefW14 ~ ageW14

ageW7 ~~ ageW7
ageW10 ~~ ageW10
ageW11 ~~ ageW11
ageW14 ~~ ageW14

ageW7 ~~ ageW10 + ageW11 + ageW14
ageW10 ~~ ageW11 + ageW14
ageW11 ~~ ageW14

# incorporate tvc - ethnicity
wstd_auth01W7 + wstd_redist_prefW7 ~ ethnic7
wstd_auth01W10 + wstd_redist_prefW10 ~ ethnic10
wstd_auth01W11 + wstd_redist_prefW11 ~ ethnic11
wstd_auth01W14 + wstd_redist_prefW14 ~ ethnic14

ethnic7 ~~ ethnic7
ethnic10 ~~ ethnic10
ethnic11 ~~ ethnic11
ethnic14 ~~ ethnic14

ethnic7 ~~ ethnic10 + ethnic11 + ethnic14
ethnic10 ~~ ethnic11 + ethnic14
ethnic11 ~~ ethnic14

# incorporate tvc – social grade
wstd_auth01W7 + wstd_redist_prefW7 ~ p_socgradeW7
wstd_auth01W10 + wstd_redist_prefW10 ~ p_socgradeW10
wstd_auth01W11 + wstd_redist_prefW11 ~ p_socgradeW11
wstd_auth01W14 + wstd_redist_prefW14 ~ p_socgradeW14

p_socgradeW7 ~~ p_socgradeW7
p_socgradeW10 ~~ p_socgradeW10
p_socgradeW11 ~~ p_socgradeW11
p_socgradeW14 ~~ p_socgradeW14

p_socgradeW7 ~~ p_socgradeW10 + p_socgradeW11 + p_socgradeW14
p_socgradeW10 ~~ p_socgradeW11 + p_socgradeW14
p_socgradeW11 ~~ p_socgradeW14

# incorporate tvc – education
wstd_auth01W7 + wstd_redist_prefW7 ~ higheduc7
wstd_auth01W10 + wstd_redist_prefW10 ~ higheduc10
wstd_auth01W11 + wstd_redist_prefW11 ~ higheduc11
wstd_auth01W14 + wstd_redist_prefW14 ~ higheduc14

higheduc7 ~~ higheduc7
higheduc10 ~~ higheduc10
higheduc11 ~~ higheduc11
higheduc14 ~~ higheduc14

higheduc7 ~~ higheduc10 + higheduc11 + higheduc14
higheduc10 ~~ higheduc11 + higheduc14
higheduc11 ~~ higheduc14

# incorporate tvc – income
wstd_auth01W7 + wstd_redist_prefW7 ~ incomerec7
wstd_auth01W10 + wstd_redist_prefW10 ~ incomerec10
wstd_auth01W11 + wstd_redist_prefW11 ~ incomerec11
wstd_auth01W14 + wstd_redist_prefW14 ~ incomerec14

incomerec7 ~~ incomerec7
incomerec10 ~~ incomerec10
incomerec11 ~~ incomerec11
incomerec14 ~~ incomerec14

incomerec7 ~~ incomerec10 + incomerec11 + incomerec14
incomerec10 ~~ incomerec11 + incomerec14
incomerec11 ~~ incomerec14

'

std_redist_CLPM_H_cov_noEQ.fit <- lavaan(std_redist_CLPM_H_cov_noEQ, 
                                     data = att_dat_high, 
                                     estimator = "MLR",
                                     missing = 'ML', meanstructure = T, 
                                     int.ov.free = T) 
summary(std_redist_CLPM_H_cov_noEQ.fit)

# Low 
std_redist_CLPM_L_cov <- '
  # Create between components (random intercepts)
  RIstd_auth01W  =~ 1*std_auth01W7 + 1*std_auth01W10 + 1*std_auth01W11 + 1*std_auth01W14 
  RIstd_redist_prefW =~ 1*std_redistSelf01W7 + 1*std_redistSelf01W10 + 1*std_redistSelf01W11 + 1*std_redistSelf01W14 
  
  # Create within-person centered variables
  wstd_auth01W7 =~ NA*std_auth01W7
  wstd_auth01W10 =~ NA*std_auth01W10
  wstd_auth01W11 =~ NA*std_auth01W11 
  wstd_auth01W14 =~ NA*std_auth01W14

  wstd_redist_prefW7 =~ NA*std_redistSelf01W7
  wstd_redist_prefW10 =~ NA*std_redistSelf01W10
  wstd_redist_prefW11 =~ NA*std_redistSelf01W11
  wstd_redist_prefW14 =~ NA*std_redistSelf01W14

  # Estimate the lagged effects between the within-person centered variables.
  wstd_auth01W10 ~ a*wstd_auth01W7 + b*wstd_redist_prefW7
  wstd_redist_prefW10 ~ c*wstd_auth01W7 + d*wstd_redist_prefW7
  wstd_auth01W11 ~ a*wstd_auth01W10 + b*wstd_redist_prefW10
  wstd_redist_prefW11 ~ c*wstd_auth01W10 + d*wstd_redist_prefW10
  wstd_auth01W14 ~ a*wstd_auth01W11 + b*wstd_redist_prefW11
  wstd_redist_prefW14 ~ c*wstd_auth01W11 + d*wstd_redist_prefW11

  # Estimate the covariance between the within-person centered variables at the first wave. 
  wstd_auth01W7 ~~ cor1*wstd_redist_prefW7 # Covariance
  
  # Estimate the covariances between the residuals of the within-person centered variables (the innovations).
  wstd_auth01W10 ~~ rcov2*wstd_redist_prefW10
  wstd_auth01W11 ~~ rcov3*wstd_redist_prefW11
  wstd_auth01W14 ~~ rcov4*wstd_redist_prefW14
  
  
  # Estimate the variance and covariance of the random intercepts. 
  RIstd_auth01W ~~ 0*RIstd_auth01W
  RIstd_redist_prefW ~~ 0*RIstd_redist_prefW
  RIstd_auth01W ~~ 0*RIstd_redist_prefW

  # Set variances of within-components at first wave to 1
  wstd_auth01W7 ~~ 1*wstd_auth01W7 # Variances
  wstd_redist_prefW7 ~~ 1*wstd_redist_prefW7 
  
  wstd_auth01W10 ~~ rvx2*wstd_auth01W10 # Residual variances
  wstd_redist_prefW10 ~~ rvy2*wstd_redist_prefW10 
  wstd_auth01W11 ~~ rvx3*wstd_auth01W11
  wstd_redist_prefW11 ~~ rvy3*wstd_redist_prefW11 
  wstd_auth01W14 ~~ rvx4*wstd_auth01W14 
  wstd_redist_prefW14 ~~ rvy4*wstd_redist_prefW14 
  
  # Fix error variance of indicators to (1 - alpha)*var
std_auth01W10 ~~ 0.0058*std_auth01W10
std_auth01W11 ~~ 0.002*std_auth01W11
std_auth01W14 ~~ 0.00725*std_auth01W14
std_redistSelf01W10 ~~ 0.01038*std_redistSelf01W10
std_redistSelf01W11 ~~ 0.00933*std_redistSelf01W11
std_redistSelf01W14 ~~ 0*std_redistSelf01W14

 # Compute correlations of within-components at each wave
  cor2 := a*c + b*d + a*d*cor1 + b*c*cor1 + rcov2
  cor3 := a*c + b*d + a*d*cor2 + b*c*cor2 + rcov3
  cor4 := a*c + b*d + a*d*cor3 + b*c*cor3 + rcov4
 
  # Contrain residual variances of within-components such that variance of each 
  # within-component equals 1
  rvx2 == 1 - (a*a + b*b + 2*a*b*cor1)
  rvy2 == 1 - (c*c + d*d + 2*c*d*cor1)
  rvx3 == 1 - (a*a + b*b + 2*a*b*cor2)
  rvy3 == 1 - (c*c + d*d + 2*c*d*cor2)
  rvx4 == 1 - (a*a + b*b + 2*a*b*cor3)
  rvy4 == 1 - (c*c + d*d + 2*c*d*cor3)
  
# incorporate tvc - age
wstd_auth01W7 + wstd_redist_prefW7 ~ ageW7
wstd_auth01W10 + wstd_redist_prefW10 ~ ageW10
wstd_auth01W11 + wstd_redist_prefW11 ~ ageW11
wstd_auth01W14 + wstd_redist_prefW14 ~ ageW14

ageW7 ~~ ageW7
ageW10 ~~ ageW10
ageW11 ~~ ageW11
ageW14 ~~ ageW14

ageW7 ~~ ageW10 + ageW11 + ageW14
ageW10 ~~ ageW11 + ageW14
ageW11 ~~ ageW14

# incorporate tvc - ethnicity
wstd_auth01W7 + wstd_redist_prefW7 ~ ethnic7
wstd_auth01W10 + wstd_redist_prefW10 ~ ethnic10
wstd_auth01W11 + wstd_redist_prefW11 ~ ethnic11
wstd_auth01W14 + wstd_redist_prefW14 ~ ethnic14

ethnic7 ~~ ethnic7
ethnic10 ~~ ethnic10
ethnic11 ~~ ethnic11
ethnic14 ~~ ethnic14

ethnic7 ~~ ethnic10 + ethnic11 + ethnic14
ethnic10 ~~ ethnic11 + ethnic14
ethnic11 ~~ ethnic14

# incorporate tvc – social grade
wstd_auth01W7 + wstd_redist_prefW7 ~ p_socgradeW7
wstd_auth01W10 + wstd_redist_prefW10 ~ p_socgradeW10
wstd_auth01W11 + wstd_redist_prefW11 ~ p_socgradeW11
wstd_auth01W14 + wstd_redist_prefW14 ~ p_socgradeW14

p_socgradeW7 ~~ p_socgradeW7
p_socgradeW10 ~~ p_socgradeW10
p_socgradeW11 ~~ p_socgradeW11
p_socgradeW14 ~~ p_socgradeW14

p_socgradeW7 ~~ p_socgradeW10 + p_socgradeW11 + p_socgradeW14
p_socgradeW10 ~~ p_socgradeW11 + p_socgradeW14
p_socgradeW11 ~~ p_socgradeW14

# incorporate tvc – education
wstd_auth01W7 + wstd_redist_prefW7 ~ higheduc7
wstd_auth01W10 + wstd_redist_prefW10 ~ higheduc10
wstd_auth01W11 + wstd_redist_prefW11 ~ higheduc11
wstd_auth01W14 + wstd_redist_prefW14 ~ higheduc14

higheduc7 ~~ higheduc7
higheduc10 ~~ higheduc10
higheduc11 ~~ higheduc11
higheduc14 ~~ higheduc14

higheduc7 ~~ higheduc10 + higheduc11 + higheduc14
higheduc10 ~~ higheduc11 + higheduc14
higheduc11 ~~ higheduc14

# incorporate tvc – income
wstd_auth01W7 + wstd_redist_prefW7 ~ incomerec7
wstd_auth01W10 + wstd_redist_prefW10 ~ incomerec10
wstd_auth01W11 + wstd_redist_prefW11 ~ incomerec11
wstd_auth01W14 + wstd_redist_prefW14 ~ incomerec14

incomerec7 ~~ incomerec7
incomerec10 ~~ incomerec10
incomerec11 ~~ incomerec11
incomerec14 ~~ incomerec14

incomerec7 ~~ incomerec10 + incomerec11 + incomerec14
incomerec10 ~~ incomerec11 + incomerec14
incomerec11 ~~ incomerec14

'


std_redist_CLPM_L_cov_noEQ <- '
  # Create between components (random intercepts)
  RIstd_auth01W  =~ 1*std_auth01W7 + 1*std_auth01W10 + 1*std_auth01W11 + 1*std_auth01W14 
  RIstd_redist_prefW =~ 1*std_redistSelf01W7 + 1*std_redistSelf01W10 + 1*std_redistSelf01W11 + 1*std_redistSelf01W14 
  
  # Create within-person centered variables
  wstd_auth01W7 =~ 1*std_auth01W7
  wstd_auth01W10 =~ 1*std_auth01W10
  wstd_auth01W11 =~ 1*std_auth01W11 
  wstd_auth01W14 =~ 1*std_auth01W14

  wstd_redist_prefW7 =~ 1*std_redistSelf01W7
  wstd_redist_prefW10 =~ 1*std_redistSelf01W10
  wstd_redist_prefW11 =~ 1*std_redistSelf01W11
  wstd_redist_prefW14 =~ 1*std_redistSelf01W14

  # Estimate the lagged effects between the within-person centered variables.
  wstd_auth01W10 + wstd_redist_prefW10 ~ wstd_auth01W7 + wstd_redist_prefW7
  wstd_auth01W11 + wstd_redist_prefW11 ~ wstd_auth01W10 + wstd_redist_prefW10
  wstd_auth01W14 + wstd_redist_prefW14 ~ wstd_auth01W11 + wstd_redist_prefW11

  # Estimate the covariance between the within-person centered variables at the first wave. 
  wstd_auth01W7 ~~ wstd_redist_prefW7 # Covariance
  
  # Estimate the covariances between the residuals of the within-person centered variables (the innovations).
  wstd_auth01W10 ~~ wstd_redist_prefW10
  wstd_auth01W11 ~~ wstd_redist_prefW11
  wstd_auth01W14 ~~ wstd_redist_prefW14
  
  
  # Constrain the variance and covariance of the random intercepts to zero  
  RIstd_auth01W ~~ 0*RIstd_auth01W
  RIstd_redist_prefW ~~ 0*RIstd_redist_prefW
  RIstd_auth01W ~~ 0*RIstd_redist_prefW

  # Estimate the (residual) variance of the within-person centered variables.
  wstd_auth01W7 ~~ wstd_auth01W7 # Variances
  wstd_redist_prefW7 ~~ wstd_redist_prefW7 
  wstd_auth01W10 ~~ wstd_auth01W10 # Residual variances
  wstd_redist_prefW10 ~~ wstd_redist_prefW10 
  wstd_auth01W11 ~~ wstd_auth01W11
  wstd_redist_prefW11 ~~ wstd_redist_prefW11 
  wstd_auth01W14 ~~ wstd_auth01W14 
  wstd_redist_prefW14 ~~ wstd_redist_prefW14 
  
  # Fix error variance of indicators to (1 - alpha)*var
std_auth01W10 ~~ 0.0058*std_auth01W10
std_auth01W11 ~~ 0.002*std_auth01W11
std_auth01W14 ~~ 0.00725*std_auth01W14
std_redistSelf01W10 ~~ 0.01038*std_redistSelf01W10
std_redistSelf01W11 ~~ 0.00933*std_redistSelf01W11
std_redistSelf01W14 ~~ 0*std_redistSelf01W14

# incorporate tvc - age
wstd_auth01W7 + wstd_redist_prefW7 ~ ageW7
wstd_auth01W10 + wstd_redist_prefW10 ~ ageW10
wstd_auth01W11 + wstd_redist_prefW11 ~ ageW11
wstd_auth01W14 + wstd_redist_prefW14 ~ ageW14

ageW7 ~~ ageW7
ageW10 ~~ ageW10
ageW11 ~~ ageW11
ageW14 ~~ ageW14

ageW7 ~~ ageW10 + ageW11 + ageW14
ageW10 ~~ ageW11 + ageW14
ageW11 ~~ ageW14

# incorporate tvc - ethnicity
wstd_auth01W7 + wstd_redist_prefW7 ~ ethnic7
wstd_auth01W10 + wstd_redist_prefW10 ~ ethnic10
wstd_auth01W11 + wstd_redist_prefW11 ~ ethnic11
wstd_auth01W14 + wstd_redist_prefW14 ~ ethnic14

ethnic7 ~~ ethnic7
ethnic10 ~~ ethnic10
ethnic11 ~~ ethnic11
ethnic14 ~~ ethnic14

ethnic7 ~~ ethnic10 + ethnic11 + ethnic14
ethnic10 ~~ ethnic11 + ethnic14
ethnic11 ~~ ethnic14

# incorporate tvc – social grade
wstd_auth01W7 + wstd_redist_prefW7 ~ p_socgradeW7
wstd_auth01W10 + wstd_redist_prefW10 ~ p_socgradeW10
wstd_auth01W11 + wstd_redist_prefW11 ~ p_socgradeW11
wstd_auth01W14 + wstd_redist_prefW14 ~ p_socgradeW14

p_socgradeW7 ~~ p_socgradeW7
p_socgradeW10 ~~ p_socgradeW10
p_socgradeW11 ~~ p_socgradeW11
p_socgradeW14 ~~ p_socgradeW14

p_socgradeW7 ~~ p_socgradeW10 + p_socgradeW11 + p_socgradeW14
p_socgradeW10 ~~ p_socgradeW11 + p_socgradeW14
p_socgradeW11 ~~ p_socgradeW14

# incorporate tvc – education
wstd_auth01W7 + wstd_redist_prefW7 ~ higheduc7
wstd_auth01W10 + wstd_redist_prefW10 ~ higheduc10
wstd_auth01W11 + wstd_redist_prefW11 ~ higheduc11
wstd_auth01W14 + wstd_redist_prefW14 ~ higheduc14

higheduc7 ~~ higheduc7
higheduc10 ~~ higheduc10
higheduc11 ~~ higheduc11
higheduc14 ~~ higheduc14

higheduc7 ~~ higheduc10 + higheduc11 + higheduc14
higheduc10 ~~ higheduc11 + higheduc14
higheduc11 ~~ higheduc14

# incorporate tvc – income
wstd_auth01W7 + wstd_redist_prefW7 ~ incomerec7
wstd_auth01W10 + wstd_redist_prefW10 ~ incomerec10
wstd_auth01W11 + wstd_redist_prefW11 ~ incomerec11
wstd_auth01W14 + wstd_redist_prefW14 ~ incomerec14

incomerec7 ~~ incomerec7
incomerec10 ~~ incomerec10
incomerec11 ~~ incomerec11
incomerec14 ~~ incomerec14

incomerec7 ~~ incomerec10 + incomerec11 + incomerec14
incomerec10 ~~ incomerec11 + incomerec14
incomerec11 ~~ incomerec14

'

std_redist_CLPM_L_cov_noEQ.fit <- lavaan(std_redist_CLPM_L_cov_noEQ, 
                                     data = att_dat_low, 
                                     estimator = "MLR",
                                     missing = 'ML', meanstructure = T, 
                                     int.ov.free = T) 
summary(std_redist_CLPM_L_cov_noEQ.fit)

std_redist_CLPM_L_cov.fit <- lavaan(std_redist_CLPM_L_cov, data = att_dat_low, 
                                estimator = "MLR",
                                missing = 'ML', 
                                meanstructure = T, 
                                int.ov.free = T) 
summary(std_redist_CLPM_L_cov.fit, standardized = T)

# Results:

standardizedsolution(std_redist_CLPM_H_cov.fit)
standardizedsolution(std_redist_CLPM_H_cov_noEQ.fit)

standardizedsolution(std_redist_CLPM_L_cov.fit)
standardizedsolution(std_redist_CLPM_L_cov_noEQ.fit)

#### Multiple-group CLPMs: Engagement - immigration ####

std_immig_attU_CLPM <- '
  # Create between components (random intercepts)
  RIstd_auth01W  =~ 1*std_auth01W7 + 1*std_auth01W10 + 1*std_auth01W11 + 1*std_auth01W14 
  RIim_prefW =~ 1*std_im_pref_2W7 + 1*std_im_pref_2W10 + 1*std_im_pref_2W11 + 1*std_im_pref_2W14 
  
  # Create within-person centered variables
  wstd_auth01W7 =~ 1*std_auth01W7
  wstd_auth01W10 =~ 1*std_auth01W10
  wstd_auth01W11 =~ 1*std_auth01W11 
  wstd_auth01W14 =~ 1*std_auth01W14

  wim_prefW7 =~ 1*std_im_pref_2W7
  wim_prefW10 =~ 1*std_im_pref_2W10
  wim_prefW11 =~ 1*std_im_pref_2W11
  wim_prefW14 =~ 1*std_im_pref_2W14

  # Estimate the lagged effects between the within-person centered variables.
  wstd_auth01W10 + wim_prefW10 ~ wstd_auth01W7 + wim_prefW7
  wstd_auth01W11 + wim_prefW11 ~ wstd_auth01W10 + wim_prefW10
  wstd_auth01W14 + wim_prefW14 ~ wstd_auth01W11 + wim_prefW11

  # Estimate the covariance between the within-person centered variables at the first wave. 
  wstd_auth01W7 ~~ wim_prefW7 # Covariance
  
  # Estimate the covariances between the residuals of the within-person centered variables (the innovations).
  wstd_auth01W10 ~~ wim_prefW10
  wstd_auth01W11 ~~ wim_prefW11
  wstd_auth01W14 ~~ wim_prefW14
  
  
  # Constrain the variance and covariance of the random intercepts to 0 
  RIstd_auth01W ~~ 0*RIstd_auth01W
  RIim_prefW ~~ 0*RIim_prefW
  RIstd_auth01W ~~ 0*RIim_prefW

  # Estimate the (residual) variance of the within-person centered variables.
  wstd_auth01W7 ~~ wstd_auth01W7 # Variances
  wim_prefW7 ~~ wim_prefW7 
  wstd_auth01W10 ~~ wstd_auth01W10 # Residual variances
  wim_prefW10 ~~ wim_prefW10 
  wstd_auth01W11 ~~ wstd_auth01W11
  wim_prefW11 ~~ wim_prefW11 
  wstd_auth01W14 ~~ wstd_auth01W14 
  wim_prefW14 ~~ wim_prefW14 
  
# Fix error variance of indicators to (1 - alpha)*var
std_auth01W7 ~~ 0.02*std_auth01W7
std_auth01W10 ~~ 0.016*std_auth01W10
std_auth01W11 ~~ 0.018*std_auth01W11
std_auth01W14 ~~ 0.019*std_auth01W14
std_im_pref_2W7 ~~ 0.0147*std_im_pref_2W7
std_im_pref_2W10 ~~ 0.000155*std_im_pref_2W10
std_im_pref_2W11 ~~ 0.000147*std_im_pref_2W11
std_im_pref_2W14 ~~ 0.000512*std_im_pref_2W14
  '
std_immig_attU_CLPM.fit <- lavaan(std_immig_attU_CLPM, 
                                  data = att_dat, 
                                  group = "polatt_group",
                                  estimator = "MLR",
                                  missing = 'ML',
                                  meanstructure = T, 
                                  int.ov.free = T) 
summary(std_immig_attU_CLPM.fit, standardized = T)

std_immig_attC_CLPM <- '
  # Create between components (random intercepts)
  RIstd_auth01W  =~ 1*std_auth01W7 + 1*std_auth01W10 + 1*std_auth01W11 + 1*std_auth01W14 
  RIim_prefW =~ 1*std_im_pref_2W7 + 1*std_im_pref_2W10 + 1*std_im_pref_2W11 + 1*std_im_pref_2W14 
  
  # Create within-person centered variables
  wstd_auth01W7 =~ 1*std_auth01W7
  wstd_auth01W10 =~ 1*std_auth01W10
  wstd_auth01W11 =~ 1*std_auth01W11 
  wstd_auth01W14 =~ 1*std_auth01W14

  wim_prefW7 =~ 1*std_im_pref_2W7
  wim_prefW10 =~ 1*std_im_pref_2W10
  wim_prefW11 =~ 1*std_im_pref_2W11
  wim_prefW14 =~ 1*std_im_pref_2W14

  # Estimate the lagged effects between the within-person centered variables.
  wstd_auth01W10 ~ c(a1, a1)*wstd_auth01W7 + c(b1, b1)*wim_prefW7
  wim_prefW10 ~ c(c1, c1)*wstd_auth01W7 + c(d1, d1)*wim_prefW7
  wstd_auth01W11 ~ c(a2, a2)*wstd_auth01W10 + c(b2, b2)*wim_prefW10
  wim_prefW11 ~ c(c2, c2)*wstd_auth01W10 + c(d2, d2)*wim_prefW10
  wstd_auth01W14 ~ c(a3, a3)*wstd_auth01W11 + c(b3, b3)*wim_prefW11
  wim_prefW14 ~ c(c3, c3)*wstd_auth01W11 + c(d3, d3)*wim_prefW11

  # Estimate the covariance between the within-person centered variables at the first wave. 
  wstd_auth01W7 ~~ wim_prefW7 # Covariance
  
  # Estimate the covariances between the residuals of the within-person centered variables (the innovations).
  wstd_auth01W10 ~~ wim_prefW10
  wstd_auth01W11 ~~ wim_prefW11
  wstd_auth01W14 ~~ wim_prefW14
  
  
  # Constrain the variance and covariance of the random intercepts to 0 
  RIstd_auth01W ~~ 0*RIstd_auth01W
  RIim_prefW ~~ 0*RIim_prefW
  RIstd_auth01W ~~ 0*RIim_prefW

  # Estimate the (residual) variance of the within-person centered variables.
  wstd_auth01W7 ~~ wstd_auth01W7 # Variances
  wim_prefW7 ~~ wim_prefW7 
  wstd_auth01W10 ~~ wstd_auth01W10 # Residual variances
  wim_prefW10 ~~ wim_prefW10 
  wstd_auth01W11 ~~ wstd_auth01W11
  wim_prefW11 ~~ wim_prefW11 
  wstd_auth01W14 ~~ wstd_auth01W14 
  wim_prefW14 ~~ wim_prefW14 
  
# Fix error variance of indicators to (1 - alpha)*var
std_auth01W7 ~~ 0.02*std_auth01W7
std_auth01W10 ~~ 0.016*std_auth01W10
std_auth01W11 ~~ 0.018*std_auth01W11
std_auth01W14 ~~ 0.019*std_auth01W14
std_im_pref_2W7 ~~ 0.0147*std_im_pref_2W7
std_im_pref_2W10 ~~ 0.000155*std_im_pref_2W10
std_im_pref_2W11 ~~ 0.000147*std_im_pref_2W11
std_im_pref_2W14 ~~ 0.000512*std_im_pref_2W14
  '
std_immig_attC_CLPM.fit <- lavaan(std_immig_attC_CLPM, 
                                  data = att_dat, 
                                  group = "polatt_group",
                                  estimator = "MLR",
                                  missing = 'ML',
                                  meanstructure = T, 
                                  int.ov.free = T) 
summary(std_immig_attC_CLPM.fit, standardized = T)

anova(std_immig_attU_CLPM.fit, std_immig_attC_CLPM.fit)

# Baseline models

std_immig_CLPM_H <- '
  # Create between components (random intercepts)
  RIstd_auth01W  =~ 1*std_auth01W7 + 1*std_auth01W10 + 1*std_auth01W11 + 1*std_auth01W14 
  RIim_prefW =~ 1*std_im_pref_2W7 + 1*std_im_pref_2W10 + 1*std_im_pref_2W11 + 1*std_im_pref_2W14 
  
  # Create within-person centered variables
  wstd_auth01W7 =~ NA*std_auth01W7
  wstd_auth01W10 =~ NA*std_auth01W10
  wstd_auth01W11 =~ NA*std_auth01W11 
  wstd_auth01W14 =~ NA*std_auth01W14

  wim_prefW7 =~ NA*std_im_pref_2W7
  wim_prefW10 =~ NA*std_im_pref_2W10
  wim_prefW11 =~ NA*std_im_pref_2W11
  wim_prefW14 =~ NA*std_im_pref_2W14

  # Estimate the lagged effects between the within-person centered variables.
  wstd_auth01W10 ~ a*wstd_auth01W7 + b*wim_prefW7
  wim_prefW10 ~ c*wstd_auth01W7 + d*wim_prefW7
  wstd_auth01W11 ~ a*wstd_auth01W10 + b*wim_prefW10
  wim_prefW11 ~ c*wstd_auth01W10 + d*wim_prefW10
  wstd_auth01W14 ~ a*wstd_auth01W11 + b*wim_prefW11
  wim_prefW14 ~ c*wstd_auth01W11 + d*wim_prefW11

  # Estimate the covariance between the within-person centered variables at the first wave. 
  wstd_auth01W7 ~~ cor1*wim_prefW7 # Covariance
  
  # Estimate the covariances between the residuals of the within-person centered variables (the innovations).
  wstd_auth01W10 ~~ rcov2*wim_prefW10
  wstd_auth01W11 ~~ rcov3*wim_prefW11
  wstd_auth01W14 ~~ rcov4*wim_prefW14
  
  
  # Constrain the variance and covariance of the random intercepts to 0 
  RIstd_auth01W ~~ 0*RIstd_auth01W
  RIim_prefW ~~ 0*RIim_prefW
  RIstd_auth01W ~~ 0*RIim_prefW

  # Set variances of within-components at first wave to 1
  wstd_auth01W7 ~~ 1*wstd_auth01W7 # Variances
  wim_prefW7 ~~ 1*wim_prefW7 
  
  
  wstd_auth01W10 ~~ rvx2*wstd_auth01W10 # Residual variances
  wim_prefW10 ~~ rvy2*wim_prefW10 
  wstd_auth01W11 ~~ rvx3*wstd_auth01W11
  wim_prefW11 ~~ rvy3*wim_prefW11 
  wstd_auth01W14 ~~ rvx4*wstd_auth01W14 
  wim_prefW14 ~~ rvy4*wim_prefW14 
  
# Fix error variance of indicators to (1 - alpha)*var
std_auth01W7 ~~ 0.02*std_auth01W7
std_auth01W10 ~~ 0.016*std_auth01W10
std_auth01W11 ~~ 0.018*std_auth01W11
std_auth01W14 ~~ 0.019*std_auth01W14
std_im_pref_2W7 ~~ 0.0147*std_im_pref_2W7
std_im_pref_2W10 ~~ 0.000155*std_im_pref_2W10
std_im_pref_2W11 ~~ 0.000147*std_im_pref_2W11
std_im_pref_2W14 ~~ 0.000512*std_im_pref_2W14

 # Compute correlations of within-components at each wave
  cor2 := a*c + b*d + a*d*cor1 + b*c*cor1 + rcov2
  cor3 := a*c + b*d + a*d*cor2 + b*c*cor2 + rcov3
  cor4 := a*c + b*d + a*d*cor3 + b*c*cor3 + rcov4
 
  # Contrain residual variances of within-components such that variance of each 
  # within-component equals 1
  rvx2 == 1 - (a*a + b*b + 2*a*b*cor1)
  rvy2 == 1 - (c*c + d*d + 2*c*d*cor1)
  rvx3 == 1 - (a*a + b*b + 2*a*b*cor2)
  rvy3 == 1 - (c*c + d*d + 2*c*d*cor2)
  rvx4 == 1 - (a*a + b*b + 2*a*b*cor3)
  rvy4 == 1 - (c*c + d*d + 2*c*d*cor3)
  '
std_immig_CLPM_H.fit <- lavaan(std_immig_CLPM_H, 
                                    data = att_dat_high,
                                    estimator = "MLR",
                                    missing = 'ML',
                                    meanstructure = T, 
                                    int.ov.free = T) 
summary(std_immig_CLPM_H.fit, standardized = T)

std_immig_CLPM_H_noEQ <- '
  # Create between components (random intercepts)
  RIstd_auth01W  =~ 1*std_auth01W7 + 1*std_auth01W10 + 1*std_auth01W11 + 1*std_auth01W14 
  RIim_prefW =~ 1*std_im_pref_2W7 + 1*std_im_pref_2W10 + 1*std_im_pref_2W11 + 1*std_im_pref_2W14 
  
  # Create within-person centered variables
  wstd_auth01W7 =~ 1*std_auth01W7
  wstd_auth01W10 =~ 1*std_auth01W10
  wstd_auth01W11 =~ 1*std_auth01W11 
  wstd_auth01W14 =~ 1*std_auth01W14

  wim_prefW7 =~ 1*std_im_pref_2W7
  wim_prefW10 =~ 1*std_im_pref_2W10
  wim_prefW11 =~ 1*std_im_pref_2W11
  wim_prefW14 =~ 1*std_im_pref_2W14

  # Estimate the lagged effects between the within-person centered variables.
  wstd_auth01W10 + wim_prefW10 ~ wstd_auth01W7 + wim_prefW7
  wstd_auth01W11 + wim_prefW11 ~ wstd_auth01W10 + wim_prefW10
  wstd_auth01W14 + wim_prefW14 ~ wstd_auth01W11 + wim_prefW11

  # Estimate the covariance between the within-person centered variables at the first wave. 
  wstd_auth01W7 ~~ wim_prefW7 # Covariance
  
  # Estimate the covariances between the residuals of the within-person centered variables (the innovations).
  wstd_auth01W10 ~~ wim_prefW10
  wstd_auth01W11 ~~ wim_prefW11
  wstd_auth01W14 ~~ wim_prefW14
  
  
  # Constrain the variance and covariance of the random intercepts to 0 
  RIstd_auth01W ~~ 0*RIstd_auth01W
  RIim_prefW ~~ 0*RIim_prefW
  RIstd_auth01W ~~ 0*RIim_prefW

  # Estimate the (residual) variance of the within-person centered variables.
  wstd_auth01W7 ~~ wstd_auth01W7 # Variances
  wim_prefW7 ~~ wim_prefW7 
  wstd_auth01W10 ~~ wstd_auth01W10 # Residual variances
  wim_prefW10 ~~ wim_prefW10 
  wstd_auth01W11 ~~ wstd_auth01W11
  wim_prefW11 ~~ wim_prefW11 
  wstd_auth01W14 ~~ wstd_auth01W14 
  wim_prefW14 ~~ wim_prefW14 
  
# Fix error variance of indicators to (1 - alpha)*var
std_auth01W7 ~~ 0.02*std_auth01W7
std_auth01W10 ~~ 0.016*std_auth01W10
std_auth01W11 ~~ 0.018*std_auth01W11
std_auth01W14 ~~ 0.019*std_auth01W14
std_im_pref_2W7 ~~ 0.0147*std_im_pref_2W7
std_im_pref_2W10 ~~ 0.000155*std_im_pref_2W10
std_im_pref_2W11 ~~ 0.000147*std_im_pref_2W11
std_im_pref_2W14 ~~ 0.000512*std_im_pref_2W14
  '
std_immig_CLPM_H_noEQ.fit <- lavaan(std_immig_CLPM_H_noEQ, 
                                  data = att_dat_high,
                                  estimator = "MLR",
                                  missing = 'ML',
                                  meanstructure = T, 
                                  int.ov.free = T) 
summary(std_immig_CLPM_H_noEQ.fit, standardized = T)

std_immig_CLPM_L <- '
  # Create between components (random intercepts)
  RIstd_auth01W  =~ 1*std_auth01W7 + 1*std_auth01W10 + 1*std_auth01W11 + 1*std_auth01W14 
  RIim_prefW =~ 1*std_im_pref_2W7 + 1*std_im_pref_2W10 + 1*std_im_pref_2W11 + 1*std_im_pref_2W14 
  
  # Create within-person centered variables
  wstd_auth01W7 =~ NA*std_auth01W7
  wstd_auth01W10 =~ NA*std_auth01W10
  wstd_auth01W11 =~ NA*std_auth01W11 
  wstd_auth01W14 =~ NA*std_auth01W14

  wim_prefW7 =~ NA*std_im_pref_2W7
  wim_prefW10 =~ NA*std_im_pref_2W10
  wim_prefW11 =~ NA*std_im_pref_2W11
  wim_prefW14 =~ NA*std_im_pref_2W14

  # Estimate the lagged effects between the within-person centered variables.
  wstd_auth01W10 ~ a*wstd_auth01W7 + b*wim_prefW7
  wim_prefW10 ~ c*wstd_auth01W7 + d*wim_prefW7
  wstd_auth01W11 ~ a*wstd_auth01W10 + b*wim_prefW10
  wim_prefW11 ~ c*wstd_auth01W10 + d*wim_prefW10
  wstd_auth01W14 ~ a*wstd_auth01W11 + b*wim_prefW11
  wim_prefW14 ~ c*wstd_auth01W11 + d*wim_prefW11

  # Estimate the covariance between the within-person centered variables at the first wave. 
  wstd_auth01W7 ~~ cor1*wim_prefW7 # Covariance
  
  # Estimate the covariances between the residuals of the within-person centered variables (the innovations).
  wstd_auth01W10 ~~ rcov2*wim_prefW10
  wstd_auth01W11 ~~ rcov3*wim_prefW11
  wstd_auth01W14 ~~ rcov4*wim_prefW14
  
  
  # Constrain the variance and covariance of the random intercepts to 0 
  RIstd_auth01W ~~ 0*RIstd_auth01W
  RIim_prefW ~~ 0*RIim_prefW
  RIstd_auth01W ~~ 0*RIim_prefW

  # Set variances of within-components at first wave to 1
  wstd_auth01W7 ~~ 1*wstd_auth01W7 # Variances
  wim_prefW7 ~~ 1*wim_prefW7 
  
  
  wstd_auth01W10 ~~ rvx2*wstd_auth01W10 # Residual variances
  wim_prefW10 ~~ rvy2*wim_prefW10 
  wstd_auth01W11 ~~ rvx3*wstd_auth01W11
  wim_prefW11 ~~ rvy3*wim_prefW11 
  wstd_auth01W14 ~~ rvx4*wstd_auth01W14 
  wim_prefW14 ~~ rvy4*wim_prefW14 
  
# Fix error variance of indicators to (1 - alpha)*var
std_auth01W7 ~~ 0.02*std_auth01W7
std_auth01W10 ~~ 0.016*std_auth01W10
std_auth01W11 ~~ 0.018*std_auth01W11
std_auth01W14 ~~ 0.019*std_auth01W14
std_im_pref_2W7 ~~ 0.0147*std_im_pref_2W7
std_im_pref_2W10 ~~ 0.000155*std_im_pref_2W10
std_im_pref_2W11 ~~ 0.000147*std_im_pref_2W11
std_im_pref_2W14 ~~ 0.000512*std_im_pref_2W14

 # Compute correlations of within-components at each wave
  cor2 := a*c + b*d + a*d*cor1 + b*c*cor1 + rcov2
  cor3 := a*c + b*d + a*d*cor2 + b*c*cor2 + rcov3
  cor4 := a*c + b*d + a*d*cor3 + b*c*cor3 + rcov4
 
  # Contrain residual variances of within-components such that variance of each 
  # within-component equals 1
  rvx2 == 1 - (a*a + b*b + 2*a*b*cor1)
  rvy2 == 1 - (c*c + d*d + 2*c*d*cor1)
  rvx3 == 1 - (a*a + b*b + 2*a*b*cor2)
  rvy3 == 1 - (c*c + d*d + 2*c*d*cor2)
  rvx4 == 1 - (a*a + b*b + 2*a*b*cor3)
  rvy4 == 1 - (c*c + d*d + 2*c*d*cor3)
  '
std_immig_CLPM_L.fit <- lavaan(std_immig_CLPM_L, 
                               data = att_dat_low,
                               estimator = "MLR",
                               missing = 'ML',
                               meanstructure = T, 
                               int.ov.free = T) 
summary(std_immig_CLPM_L.fit, standardized = T)

std_immig_CLPM_L_noEQ <- '
  # Create between components (random intercepts)
  RIstd_auth01W  =~ 1*std_auth01W7 + 1*std_auth01W10 + 1*std_auth01W11 + 1*std_auth01W14 
  RIim_prefW =~ 1*std_im_pref_2W7 + 1*std_im_pref_2W10 + 1*std_im_pref_2W11 + 1*std_im_pref_2W14 
  
  # Create within-person centered variables
  wstd_auth01W7 =~ 1*std_auth01W7
  wstd_auth01W10 =~ 1*std_auth01W10
  wstd_auth01W11 =~ 1*std_auth01W11 
  wstd_auth01W14 =~ 1*std_auth01W14

  wim_prefW7 =~ 1*std_im_pref_2W7
  wim_prefW10 =~ 1*std_im_pref_2W10
  wim_prefW11 =~ 1*std_im_pref_2W11
  wim_prefW14 =~ 1*std_im_pref_2W14

  # Estimate the lagged effects between the within-person centered variables.
  wstd_auth01W10 + wim_prefW10 ~ wstd_auth01W7 + wim_prefW7
  wstd_auth01W11 + wim_prefW11 ~ wstd_auth01W10 + wim_prefW10
  wstd_auth01W14 + wim_prefW14 ~ wstd_auth01W11 + wim_prefW11

  # Estimate the covariance between the within-person centered variables at the first wave. 
  wstd_auth01W7 ~~ wim_prefW7 # Covariance
  
  # Estimate the covariances between the residuals of the within-person centered variables (the innovations).
  wstd_auth01W10 ~~ wim_prefW10
  wstd_auth01W11 ~~ wim_prefW11
  wstd_auth01W14 ~~ wim_prefW14
  
  
  # Constrain the variance and covariance of the random intercepts to 0 
  RIstd_auth01W ~~ 0*RIstd_auth01W
  RIim_prefW ~~ 0*RIim_prefW
  RIstd_auth01W ~~ 0*RIim_prefW

  # Estimate the (residual) variance of the within-person centered variables.
  wstd_auth01W7 ~~ wstd_auth01W7 # Variances
  wim_prefW7 ~~ wim_prefW7 
  wstd_auth01W10 ~~ wstd_auth01W10 # Residual variances
  wim_prefW10 ~~ wim_prefW10 
  wstd_auth01W11 ~~ wstd_auth01W11
  wim_prefW11 ~~ wim_prefW11 
  wstd_auth01W14 ~~ wstd_auth01W14 
  wim_prefW14 ~~ wim_prefW14 
  
# Fix error variance of indicators to (1 - alpha)*var
std_auth01W7 ~~ 0.02*std_auth01W7
std_auth01W10 ~~ 0.016*std_auth01W10
std_auth01W11 ~~ 0.018*std_auth01W11
std_auth01W14 ~~ 0.019*std_auth01W14
std_im_pref_2W7 ~~ 0.0147*std_im_pref_2W7
std_im_pref_2W10 ~~ 0.000155*std_im_pref_2W10
std_im_pref_2W11 ~~ 0.000147*std_im_pref_2W11
std_im_pref_2W14 ~~ 0.000512*std_im_pref_2W14
  '
std_immig_CLPM_L_noEQ.fit <- lavaan(std_immig_CLPM_L_noEQ, 
                                    data = att_dat_low,
                                    estimator = "MLR",
                                    missing = 'ML',
                                    meanstructure = T, 
                                    int.ov.free = T) 
summary(std_immig_CLPM_L_noEQ.fit, standardized = T)

# Model fit: 

fitmeasures(std_immig_CLPM_H.fit)
fitmeasures(std_immig_CLPM_H_noEQ.fit)
fitmeasures(std_immig_CLPM_L.fit)
fitmeasures(std_immig_CLPM_L_noEQ.fit)

# Results:
standardizedsolution(std_immig_CLPM_H.fit)
standardizedsolution(std_immig_CLPM_H_noEQ.fit)
standardizedsolution(std_immig_CLPM_L.fit)
standardizedsolution(std_immig_CLPM_L_noEQ.fit)

# Covariate models 

std_immig_CLPM_H_cov <- '
  # Create between components (random intercepts)
  RIstd_auth01W  =~ 1*std_auth01W7 + 1*std_auth01W10 + 1*std_auth01W11 + 1*std_auth01W14 
  RIim_prefW =~ 1*std_im_pref_2W7 + 1*std_im_pref_2W10 + 1*std_im_pref_2W11 + 1*std_im_pref_2W14 
  
  # Create within-person centered variables
  wstd_auth01W7 =~ NA*std_auth01W7
  wstd_auth01W10 =~ NA*std_auth01W10
  wstd_auth01W11 =~ NA*std_auth01W11 
  wstd_auth01W14 =~ NA*std_auth01W14

  wim_prefW7 =~ NA*std_im_pref_2W7
  wim_prefW10 =~ NA*std_im_pref_2W10
  wim_prefW11 =~ NA*std_im_pref_2W11
  wim_prefW14 =~ NA*std_im_pref_2W14

  # Estimate the lagged effects between the within-person centered variables.
  wstd_auth01W10 ~ a*wstd_auth01W7 + b*wim_prefW7
  wim_prefW10 ~ c*wstd_auth01W7 + d*wim_prefW7
  wstd_auth01W11 ~ a*wstd_auth01W10 + b*wim_prefW10
  wim_prefW11 ~ c*wstd_auth01W10 + d*wim_prefW10
  wstd_auth01W14 ~ a*wstd_auth01W11 + b*wim_prefW11
  wim_prefW14 ~ c*wstd_auth01W11 + d*wim_prefW11

  # Estimate the covariance between the within-person centered variables at the first wave. 
  wstd_auth01W7 ~~ cor1*wim_prefW7 # Covariance
  
  # Estimate the covariances between the residuals of the within-person centered variables (the innovations).
  wstd_auth01W10 ~~ rcov2*wim_prefW10
  wstd_auth01W11 ~~ rcov3*wim_prefW11
  wstd_auth01W14 ~~ rcov4*wim_prefW14
  
  
  # Constrain the variance and covariance of the random intercepts to 0 
  RIstd_auth01W ~~ 0*RIstd_auth01W
  RIim_prefW ~~ 0*RIim_prefW
  RIstd_auth01W ~~ 0*RIim_prefW

  # Set variances of within-components at first wave to 1
  wstd_auth01W7 ~~ 1*wstd_auth01W7 # Variances
  wim_prefW7 ~~ 1*wim_prefW7 
  
  
  wstd_auth01W10 ~~ rvx2*wstd_auth01W10 # Residual variances
  wim_prefW10 ~~ rvy2*wim_prefW10 
  wstd_auth01W11 ~~ rvx3*wstd_auth01W11
  wim_prefW11 ~~ rvy3*wim_prefW11 
  wstd_auth01W14 ~~ rvx4*wstd_auth01W14 
  wim_prefW14 ~~ rvy4*wim_prefW14 
  
# Fix error variance of indicators to (1 - alpha)*var
std_auth01W7 ~~ 0.02*std_auth01W7
std_auth01W10 ~~ 0.016*std_auth01W10
std_auth01W11 ~~ 0.018*std_auth01W11
std_auth01W14 ~~ 0.019*std_auth01W14
std_im_pref_2W7 ~~ 0.0147*std_im_pref_2W7
std_im_pref_2W10 ~~ 0.000155*std_im_pref_2W10
std_im_pref_2W11 ~~ 0.000147*std_im_pref_2W11
std_im_pref_2W14 ~~ 0.000512*std_im_pref_2W14

 # Compute correlations of within-components at each wave
  cor2 := a*c + b*d + a*d*cor1 + b*c*cor1 + rcov2
  cor3 := a*c + b*d + a*d*cor2 + b*c*cor2 + rcov3
  cor4 := a*c + b*d + a*d*cor3 + b*c*cor3 + rcov4
 
  # Contrain residual variances of within-components such that variance of each 
  # within-component equals 1
  rvx2 == 1 - (a*a + b*b + 2*a*b*cor1)
  rvy2 == 1 - (c*c + d*d + 2*c*d*cor1)
  rvx3 == 1 - (a*a + b*b + 2*a*b*cor2)
  rvy3 == 1 - (c*c + d*d + 2*c*d*cor2)
  rvx4 == 1 - (a*a + b*b + 2*a*b*cor3)
  rvy4 == 1 - (c*c + d*d + 2*c*d*cor3)
  
  # incorporate tvc - age
wstd_auth01W7 + wim_prefW7 ~ ageW7
wstd_auth01W10 + wim_prefW10 ~ ageW10
wstd_auth01W11 + wim_prefW11 ~ ageW11
wstd_auth01W14 + wim_prefW14 ~ ageW14

ageW7 ~~ ageW7
ageW10 ~~ ageW10
ageW11 ~~ ageW11
ageW14 ~~ ageW14

ageW7 ~~ ageW10 + ageW11 + ageW14
ageW10 ~~ ageW11 + ageW14
ageW11 ~~ ageW14

# incorporate tvc - ethnicity
wstd_auth01W7 + wim_prefW7 ~ ethnic7
wstd_auth01W10 + wim_prefW10 ~ ethnic10
wstd_auth01W11 + wim_prefW11 ~ ethnic11
wstd_auth01W14 + wim_prefW14 ~ ethnic14

ethnic7 ~~ ethnic7
ethnic10 ~~ ethnic10
ethnic11 ~~ ethnic11
ethnic14 ~~ ethnic14

ethnic7 ~~ ethnic10 + ethnic11 + ethnic14
ethnic10 ~~ ethnic11 + ethnic14
ethnic11 ~~ ethnic14

# incorporate tvc – social grade
wstd_auth01W7 + wim_prefW7 ~ p_socgradeW7
wstd_auth01W10 + wim_prefW10 ~ p_socgradeW10
wstd_auth01W11 + wim_prefW11 ~ p_socgradeW11
wstd_auth01W14 + wim_prefW14 ~ p_socgradeW14

p_socgradeW7 ~~ p_socgradeW7
p_socgradeW10 ~~ p_socgradeW10
p_socgradeW11 ~~ p_socgradeW11
p_socgradeW14 ~~ p_socgradeW14

p_socgradeW7 ~~ p_socgradeW10 + p_socgradeW11 + p_socgradeW14
p_socgradeW10 ~~ p_socgradeW11 + p_socgradeW14
p_socgradeW11 ~~ p_socgradeW14

# incorporate tvc – education
wstd_auth01W7 + wim_prefW7 ~ higheduc7
wstd_auth01W10 + wim_prefW10 ~ higheduc10
wstd_auth01W11 + wim_prefW11 ~ higheduc11
wstd_auth01W14 + wim_prefW14 ~ higheduc14

higheduc7 ~~ higheduc7
higheduc10 ~~ higheduc10
higheduc11 ~~ higheduc11
higheduc14 ~~ higheduc14

higheduc7 ~~ higheduc10 + higheduc11 + higheduc14
higheduc10 ~~ higheduc11 + higheduc14
higheduc11 ~~ higheduc14

# incorporate tvc – income
wstd_auth01W7 + wim_prefW7 ~ incomerec7
wstd_auth01W10 + wim_prefW10 ~ incomerec10
wstd_auth01W11 + wim_prefW11 ~ incomerec11
wstd_auth01W14 + wim_prefW14 ~ incomerec14

incomerec7 ~~ incomerec7
incomerec10 ~~ incomerec10
incomerec11 ~~ incomerec11
incomerec14 ~~ incomerec14

incomerec7 ~~ incomerec10 + incomerec11 + incomerec14
incomerec10 ~~ incomerec11 + incomerec14
incomerec11 ~~ incomerec14


  '
std_immig_CLPM_H_cov.fit <- lavaan(std_immig_CLPM_H_cov, 
                               data = att_dat_high,
                               estimator = "MLR",
                               missing = 'ML',
                               meanstructure = T, 
                               int.ov.free = T) 
summary(std_immig_CLPM_H_cov.fit, standardized = T)



std_immig_CLPM_H_noEQ_cov <- '
  # Create between components (random intercepts)
  RIstd_auth01W  =~ 1*std_auth01W7 + 1*std_auth01W10 + 1*std_auth01W11 + 1*std_auth01W14 
  RIim_prefW =~ 1*std_im_pref_2W7 + 1*std_im_pref_2W10 + 1*std_im_pref_2W11 + 1*std_im_pref_2W14 
  
  # Create within-person centered variables
  wstd_auth01W7 =~ 1*std_auth01W7
  wstd_auth01W10 =~ 1*std_auth01W10
  wstd_auth01W11 =~ 1*std_auth01W11 
  wstd_auth01W14 =~ 1*std_auth01W14

  wim_prefW7 =~ 1*std_im_pref_2W7
  wim_prefW10 =~ 1*std_im_pref_2W10
  wim_prefW11 =~ 1*std_im_pref_2W11
  wim_prefW14 =~ 1*std_im_pref_2W14

  # Estimate the lagged effects between the within-person centered variables.
  wstd_auth01W10 + wim_prefW10 ~ wstd_auth01W7 + wim_prefW7
  wstd_auth01W11 + wim_prefW11 ~ wstd_auth01W10 + wim_prefW10
  wstd_auth01W14 + wim_prefW14 ~ wstd_auth01W11 + wim_prefW11

  # Estimate the covariance between the within-person centered variables at the first wave. 
  wstd_auth01W7 ~~ wim_prefW7 # Covariance
  
  # Estimate the covariances between the residuals of the within-person centered variables (the innovations).
  wstd_auth01W10 ~~ wim_prefW10
  wstd_auth01W11 ~~ wim_prefW11
  wstd_auth01W14 ~~ wim_prefW14
  
  
  # Constrain the variance and covariance of the random intercepts to 0 
  RIstd_auth01W ~~ 0*RIstd_auth01W
  RIim_prefW ~~ 0*RIim_prefW
  RIstd_auth01W ~~ 0*RIim_prefW

  # Estimate the (residual) variance of the within-person centered variables.
  wstd_auth01W7 ~~ wstd_auth01W7 # Variances
  wim_prefW7 ~~ wim_prefW7 
  wstd_auth01W10 ~~ wstd_auth01W10 # Residual variances
  wim_prefW10 ~~ wim_prefW10 
  wstd_auth01W11 ~~ wstd_auth01W11
  wim_prefW11 ~~ wim_prefW11 
  wstd_auth01W14 ~~ wstd_auth01W14 
  wim_prefW14 ~~ wim_prefW14 
  
# Fix error variance of indicators to (1 - alpha)*var
std_auth01W7 ~~ 0.02*std_auth01W7
std_auth01W10 ~~ 0.016*std_auth01W10
std_auth01W11 ~~ 0.018*std_auth01W11
std_auth01W14 ~~ 0.019*std_auth01W14
std_im_pref_2W7 ~~ 0.0147*std_im_pref_2W7
std_im_pref_2W10 ~~ 0.000155*std_im_pref_2W10
std_im_pref_2W11 ~~ 0.000147*std_im_pref_2W11
std_im_pref_2W14 ~~ 0.000512*std_im_pref_2W14

  
  # incorporate tvc - age
wstd_auth01W7 + wim_prefW7 ~ ageW7
wstd_auth01W10 + wim_prefW10 ~ ageW10
wstd_auth01W11 + wim_prefW11 ~ ageW11
wstd_auth01W14 + wim_prefW14 ~ ageW14

ageW7 ~~ ageW7
ageW10 ~~ ageW10
ageW11 ~~ ageW11
ageW14 ~~ ageW14

ageW7 ~~ ageW10 + ageW11 + ageW14
ageW10 ~~ ageW11 + ageW14
ageW11 ~~ ageW14

# incorporate tvc - ethnicity
wstd_auth01W7 + wim_prefW7 ~ ethnic7
wstd_auth01W10 + wim_prefW10 ~ ethnic10
wstd_auth01W11 + wim_prefW11 ~ ethnic11
wstd_auth01W14 + wim_prefW14 ~ ethnic14

ethnic7 ~~ ethnic7
ethnic10 ~~ ethnic10
ethnic11 ~~ ethnic11
ethnic14 ~~ ethnic14

ethnic7 ~~ ethnic10 + ethnic11 + ethnic14
ethnic10 ~~ ethnic11 + ethnic14
ethnic11 ~~ ethnic14

# incorporate tvc – social grade
wstd_auth01W7 + wim_prefW7 ~ p_socgradeW7
wstd_auth01W10 + wim_prefW10 ~ p_socgradeW10
wstd_auth01W11 + wim_prefW11 ~ p_socgradeW11
wstd_auth01W14 + wim_prefW14 ~ p_socgradeW14

p_socgradeW7 ~~ p_socgradeW7
p_socgradeW10 ~~ p_socgradeW10
p_socgradeW11 ~~ p_socgradeW11
p_socgradeW14 ~~ p_socgradeW14

p_socgradeW7 ~~ p_socgradeW10 + p_socgradeW11 + p_socgradeW14
p_socgradeW10 ~~ p_socgradeW11 + p_socgradeW14
p_socgradeW11 ~~ p_socgradeW14

# incorporate tvc – education
wstd_auth01W7 + wim_prefW7 ~ higheduc7
wstd_auth01W10 + wim_prefW10 ~ higheduc10
wstd_auth01W11 + wim_prefW11 ~ higheduc11
wstd_auth01W14 + wim_prefW14 ~ higheduc14

higheduc7 ~~ higheduc7
higheduc10 ~~ higheduc10
higheduc11 ~~ higheduc11
higheduc14 ~~ higheduc14

higheduc7 ~~ higheduc10 + higheduc11 + higheduc14
higheduc10 ~~ higheduc11 + higheduc14
higheduc11 ~~ higheduc14

# incorporate tvc – income
wstd_auth01W7 + wim_prefW7 ~ incomerec7
wstd_auth01W10 + wim_prefW10 ~ incomerec10
wstd_auth01W11 + wim_prefW11 ~ incomerec11
wstd_auth01W14 + wim_prefW14 ~ incomerec14

incomerec7 ~~ incomerec7
incomerec10 ~~ incomerec10
incomerec11 ~~ incomerec11
incomerec14 ~~ incomerec14

incomerec7 ~~ incomerec10 + incomerec11 + incomerec14
incomerec10 ~~ incomerec11 + incomerec14
incomerec11 ~~ incomerec14
  '
std_immig_CLPM_H_noEQ_cov.fit <- lavaan(std_immig_CLPM_H_noEQ_cov, 
                                    data = att_dat_high,
                                    estimator = "MLR",
                                    missing = 'ML',
                                    meanstructure = T, 
                                    int.ov.free = T) 
summary(std_immig_CLPM_H_noEQ_cov.fit, standardized = T)

std_immig_CLPM_L_cov <- '
  # Create between components (random intercepts)
  RIstd_auth01W  =~ 1*std_auth01W7 + 1*std_auth01W10 + 1*std_auth01W11 + 1*std_auth01W14 
  RIim_prefW =~ 1*std_im_pref_2W7 + 1*std_im_pref_2W10 + 1*std_im_pref_2W11 + 1*std_im_pref_2W14 
  
  # Create within-person centered variables
  wstd_auth01W7 =~ NA*std_auth01W7
  wstd_auth01W10 =~ NA*std_auth01W10
  wstd_auth01W11 =~ NA*std_auth01W11 
  wstd_auth01W14 =~ NA*std_auth01W14

  wim_prefW7 =~ NA*std_im_pref_2W7
  wim_prefW10 =~ NA*std_im_pref_2W10
  wim_prefW11 =~ NA*std_im_pref_2W11
  wim_prefW14 =~ NA*std_im_pref_2W14

  # Estimate the lagged effects between the within-person centered variables.
  wstd_auth01W10 ~ a*wstd_auth01W7 + b*wim_prefW7
  wim_prefW10 ~ c*wstd_auth01W7 + d*wim_prefW7
  wstd_auth01W11 ~ a*wstd_auth01W10 + b*wim_prefW10
  wim_prefW11 ~ c*wstd_auth01W10 + d*wim_prefW10
  wstd_auth01W14 ~ a*wstd_auth01W11 + b*wim_prefW11
  wim_prefW14 ~ c*wstd_auth01W11 + d*wim_prefW11

  # Estimate the covariance between the within-person centered variables at the first wave. 
  wstd_auth01W7 ~~ cor1*wim_prefW7 # Covariance
  
  # Estimate the covariances between the residuals of the within-person centered variables (the innovations).
  wstd_auth01W10 ~~ rcov2*wim_prefW10
  wstd_auth01W11 ~~ rcov3*wim_prefW11
  wstd_auth01W14 ~~ rcov4*wim_prefW14
  
  
  # Constrain the variance and covariance of the random intercepts to 0 
  RIstd_auth01W ~~ 0*RIstd_auth01W
  RIim_prefW ~~ 0*RIim_prefW
  RIstd_auth01W ~~ 0*RIim_prefW

  # Set variances of within-components at first wave to 1
  wstd_auth01W7 ~~ 1*wstd_auth01W7 # Variances
  wim_prefW7 ~~ 1*wim_prefW7 
  
  
  wstd_auth01W10 ~~ rvx2*wstd_auth01W10 # Residual variances
  wim_prefW10 ~~ rvy2*wim_prefW10 
  wstd_auth01W11 ~~ rvx3*wstd_auth01W11
  wim_prefW11 ~~ rvy3*wim_prefW11 
  wstd_auth01W14 ~~ rvx4*wstd_auth01W14 
  wim_prefW14 ~~ rvy4*wim_prefW14 
  
# Fix error variance of indicators to (1 - alpha)*var
std_auth01W7 ~~ 0.02*std_auth01W7
std_auth01W10 ~~ 0.016*std_auth01W10
std_auth01W11 ~~ 0.018*std_auth01W11
std_auth01W14 ~~ 0.019*std_auth01W14
std_im_pref_2W7 ~~ 0.0147*std_im_pref_2W7
std_im_pref_2W10 ~~ 0.000155*std_im_pref_2W10
std_im_pref_2W11 ~~ 0.000147*std_im_pref_2W11
std_im_pref_2W14 ~~ 0.000512*std_im_pref_2W14

 # Compute correlations of within-components at each wave
  cor2 := a*c + b*d + a*d*cor1 + b*c*cor1 + rcov2
  cor3 := a*c + b*d + a*d*cor2 + b*c*cor2 + rcov3
  cor4 := a*c + b*d + a*d*cor3 + b*c*cor3 + rcov4
 
  # Contrain residual variances of within-components such that variance of each 
  # within-component equals 1
  rvx2 == 1 - (a*a + b*b + 2*a*b*cor1)
  rvy2 == 1 - (c*c + d*d + 2*c*d*cor1)
  rvx3 == 1 - (a*a + b*b + 2*a*b*cor2)
  rvy3 == 1 - (c*c + d*d + 2*c*d*cor2)
  rvx4 == 1 - (a*a + b*b + 2*a*b*cor3)
  rvy4 == 1 - (c*c + d*d + 2*c*d*cor3)
  
  # incorporate tvc - age
wstd_auth01W7 + wim_prefW7 ~ ageW7
wstd_auth01W10 + wim_prefW10 ~ ageW10
wstd_auth01W11 + wim_prefW11 ~ ageW11
wstd_auth01W14 + wim_prefW14 ~ ageW14

ageW7 ~~ ageW7
ageW10 ~~ ageW10
ageW11 ~~ ageW11
ageW14 ~~ ageW14

ageW7 ~~ ageW10 + ageW11 + ageW14
ageW10 ~~ ageW11 + ageW14
ageW11 ~~ ageW14

# incorporate tvc - ethnicity
wstd_auth01W7 + wim_prefW7 ~ ethnic7
wstd_auth01W10 + wim_prefW10 ~ ethnic10
wstd_auth01W11 + wim_prefW11 ~ ethnic11
wstd_auth01W14 + wim_prefW14 ~ ethnic14

ethnic7 ~~ ethnic7
ethnic10 ~~ ethnic10
ethnic11 ~~ ethnic11
ethnic14 ~~ ethnic14

ethnic7 ~~ ethnic10 + ethnic11 + ethnic14
ethnic10 ~~ ethnic11 + ethnic14
ethnic11 ~~ ethnic14

# incorporate tvc – social grade
wstd_auth01W7 + wim_prefW7 ~ p_socgradeW7
wstd_auth01W10 + wim_prefW10 ~ p_socgradeW10
wstd_auth01W11 + wim_prefW11 ~ p_socgradeW11
wstd_auth01W14 + wim_prefW14 ~ p_socgradeW14

p_socgradeW7 ~~ p_socgradeW7
p_socgradeW10 ~~ p_socgradeW10
p_socgradeW11 ~~ p_socgradeW11
p_socgradeW14 ~~ p_socgradeW14

p_socgradeW7 ~~ p_socgradeW10 + p_socgradeW11 + p_socgradeW14
p_socgradeW10 ~~ p_socgradeW11 + p_socgradeW14
p_socgradeW11 ~~ p_socgradeW14

# incorporate tvc – education
wstd_auth01W7 + wim_prefW7 ~ higheduc7
wstd_auth01W10 + wim_prefW10 ~ higheduc10
wstd_auth01W11 + wim_prefW11 ~ higheduc11
wstd_auth01W14 + wim_prefW14 ~ higheduc14

higheduc7 ~~ higheduc7
higheduc10 ~~ higheduc10
higheduc11 ~~ higheduc11
higheduc14 ~~ higheduc14

higheduc7 ~~ higheduc10 + higheduc11 + higheduc14
higheduc10 ~~ higheduc11 + higheduc14
higheduc11 ~~ higheduc14

# incorporate tvc – income
wstd_auth01W7 + wim_prefW7 ~ incomerec7
wstd_auth01W10 + wim_prefW10 ~ incomerec10
wstd_auth01W11 + wim_prefW11 ~ incomerec11
wstd_auth01W14 + wim_prefW14 ~ incomerec14

incomerec7 ~~ incomerec7
incomerec10 ~~ incomerec10
incomerec11 ~~ incomerec11
incomerec14 ~~ incomerec14

incomerec7 ~~ incomerec10 + incomerec11 + incomerec14
incomerec10 ~~ incomerec11 + incomerec14
incomerec11 ~~ incomerec14


  '
std_immig_CLPM_L_cov.fit <- lavaan(std_immig_CLPM_L_cov, 
                                   data = att_dat_low,
                                   estimator = "MLR",
                                   missing = 'ML',
                                   meanstructure = T, 
                                   int.ov.free = T) 
summary(std_immig_CLPM_L_cov.fit, standardized = T)



std_immig_CLPM_L_noEQ_cov <- '
  # Create between components (random intercepts)
  RIstd_auth01W  =~ 1*std_auth01W7 + 1*std_auth01W10 + 1*std_auth01W11 + 1*std_auth01W14 
  RIim_prefW =~ 1*std_im_pref_2W7 + 1*std_im_pref_2W10 + 1*std_im_pref_2W11 + 1*std_im_pref_2W14 
  
  # Create within-person centered variables
  wstd_auth01W7 =~ 1*std_auth01W7
  wstd_auth01W10 =~ 1*std_auth01W10
  wstd_auth01W11 =~ 1*std_auth01W11 
  wstd_auth01W14 =~ 1*std_auth01W14

  wim_prefW7 =~ 1*std_im_pref_2W7
  wim_prefW10 =~ 1*std_im_pref_2W10
  wim_prefW11 =~ 1*std_im_pref_2W11
  wim_prefW14 =~ 1*std_im_pref_2W14

  # Estimate the lagged effects between the within-person centered variables.
  wstd_auth01W10 + wim_prefW10 ~ wstd_auth01W7 + wim_prefW7
  wstd_auth01W11 + wim_prefW11 ~ wstd_auth01W10 + wim_prefW10
  wstd_auth01W14 + wim_prefW14 ~ wstd_auth01W11 + wim_prefW11

  # Estimate the covariance between the within-person centered variables at the first wave. 
  wstd_auth01W7 ~~ wim_prefW7 # Covariance
  
  # Estimate the covariances between the residuals of the within-person centered variables (the innovations).
  wstd_auth01W10 ~~ wim_prefW10
  wstd_auth01W11 ~~ wim_prefW11
  wstd_auth01W14 ~~ wim_prefW14
  
  
  # Constrain the variance and covariance of the random intercepts to 0 
  RIstd_auth01W ~~ 0*RIstd_auth01W
  RIim_prefW ~~ 0*RIim_prefW
  RIstd_auth01W ~~ 0*RIim_prefW

  # Estimate the (residual) variance of the within-person centered variables.
  wstd_auth01W7 ~~ wstd_auth01W7 # Variances
  wim_prefW7 ~~ wim_prefW7 
  wstd_auth01W10 ~~ wstd_auth01W10 # Residual variances
  wim_prefW10 ~~ wim_prefW10 
  wstd_auth01W11 ~~ wstd_auth01W11
  wim_prefW11 ~~ wim_prefW11 
  wstd_auth01W14 ~~ wstd_auth01W14 
  wim_prefW14 ~~ wim_prefW14 
  
# Fix error variance of indicators to (1 - alpha)*var
std_auth01W7 ~~ 0.02*std_auth01W7
std_auth01W10 ~~ 0.016*std_auth01W10
std_auth01W11 ~~ 0.018*std_auth01W11
std_auth01W14 ~~ 0.019*std_auth01W14
std_im_pref_2W7 ~~ 0.0147*std_im_pref_2W7
std_im_pref_2W10 ~~ 0.000155*std_im_pref_2W10
std_im_pref_2W11 ~~ 0.000147*std_im_pref_2W11
std_im_pref_2W14 ~~ 0.000512*std_im_pref_2W14

  
  # incorporate tvc - age
wstd_auth01W7 + wim_prefW7 ~ ageW7
wstd_auth01W10 + wim_prefW10 ~ ageW10
wstd_auth01W11 + wim_prefW11 ~ ageW11
wstd_auth01W14 + wim_prefW14 ~ ageW14

ageW7 ~~ ageW7
ageW10 ~~ ageW10
ageW11 ~~ ageW11
ageW14 ~~ ageW14

ageW7 ~~ ageW10 + ageW11 + ageW14
ageW10 ~~ ageW11 + ageW14
ageW11 ~~ ageW14

# incorporate tvc - ethnicity
wstd_auth01W7 + wim_prefW7 ~ ethnic7
wstd_auth01W10 + wim_prefW10 ~ ethnic10
wstd_auth01W11 + wim_prefW11 ~ ethnic11
wstd_auth01W14 + wim_prefW14 ~ ethnic14

ethnic7 ~~ ethnic7
ethnic10 ~~ ethnic10
ethnic11 ~~ ethnic11
ethnic14 ~~ ethnic14

ethnic7 ~~ ethnic10 + ethnic11 + ethnic14
ethnic10 ~~ ethnic11 + ethnic14
ethnic11 ~~ ethnic14

# incorporate tvc – social grade
wstd_auth01W7 + wim_prefW7 ~ p_socgradeW7
wstd_auth01W10 + wim_prefW10 ~ p_socgradeW10
wstd_auth01W11 + wim_prefW11 ~ p_socgradeW11
wstd_auth01W14 + wim_prefW14 ~ p_socgradeW14

p_socgradeW7 ~~ p_socgradeW7
p_socgradeW10 ~~ p_socgradeW10
p_socgradeW11 ~~ p_socgradeW11
p_socgradeW14 ~~ p_socgradeW14

p_socgradeW7 ~~ p_socgradeW10 + p_socgradeW11 + p_socgradeW14
p_socgradeW10 ~~ p_socgradeW11 + p_socgradeW14
p_socgradeW11 ~~ p_socgradeW14

# incorporate tvc – education
wstd_auth01W7 + wim_prefW7 ~ higheduc7
wstd_auth01W10 + wim_prefW10 ~ higheduc10
wstd_auth01W11 + wim_prefW11 ~ higheduc11
wstd_auth01W14 + wim_prefW14 ~ higheduc14

higheduc7 ~~ higheduc7
higheduc10 ~~ higheduc10
higheduc11 ~~ higheduc11
higheduc14 ~~ higheduc14

higheduc7 ~~ higheduc10 + higheduc11 + higheduc14
higheduc10 ~~ higheduc11 + higheduc14
higheduc11 ~~ higheduc14

# incorporate tvc – income
wstd_auth01W7 + wim_prefW7 ~ incomerec7
wstd_auth01W10 + wim_prefW10 ~ incomerec10
wstd_auth01W11 + wim_prefW11 ~ incomerec11
wstd_auth01W14 + wim_prefW14 ~ incomerec14

incomerec7 ~~ incomerec7
incomerec10 ~~ incomerec10
incomerec11 ~~ incomerec11
incomerec14 ~~ incomerec14

incomerec7 ~~ incomerec10 + incomerec11 + incomerec14
incomerec10 ~~ incomerec11 + incomerec14
incomerec11 ~~ incomerec14
  '
std_immig_CLPM_L_noEQ_cov.fit <- lavaan(std_immig_CLPM_L_noEQ_cov, 
                                        data = att_dat_low,
                                        estimator = "MLR",
                                        missing = 'ML',
                                        meanstructure = T, 
                                        int.ov.free = T) 
summary(std_immig_CLPM_L_noEQ_cov.fit, standardized = T)

# Results

standardizedSolution(std_immig_CLPM_H_cov.fit)
standardizedSolution(std_immig_CLPM_H_noEQ_cov.fit)

standardizedSolution(std_immig_CLPM_L_cov.fit)
standardizedSolution(std_immig_CLPM_L_noEQ_cov.fit)


#### Multiple-group CLPMs: Engagement - EU integration ####

std_eu_attU_CLPM <- '
  # Create between components (random intercepts)
  RIstd_auth01W  =~ 1*std_auth01W7 + 1*std_auth01W10 + 1*std_auth01W11 + 1*std_auth01W14 
  RIstd_eu_intW =~ 1*std_eu_intW7 + 1*std_eu_intW10 + 1*std_eu_intW11 + 1*std_eu_intW14 
  
  # Create within-person centered variables
  wstd_auth01W7 =~ 1*std_auth01W7
  wstd_auth01W10 =~ 1*std_auth01W10
  wstd_auth01W11 =~ 1*std_auth01W11 
  wstd_auth01W14 =~ 1*std_auth01W14

  wstd_eu_intW7 =~ 1*std_eu_intW7
  wstd_eu_intW10 =~ 1*std_eu_intW10
  wstd_eu_intW11 =~ 1*std_eu_intW11
  wstd_eu_intW14 =~ 1*std_eu_intW14

  # Estimate the lagged effects between the within-person centered variables.
  wstd_auth01W10 + wstd_eu_intW10 ~ wstd_auth01W7 + wstd_eu_intW7
  wstd_auth01W11 + wstd_eu_intW11 ~ wstd_auth01W10 + wstd_eu_intW10
  wstd_auth01W14 + wstd_eu_intW14 ~ wstd_auth01W11 + wstd_eu_intW11

  # Estimate the covariance between the within-person centered variables at the first wave. 
  wstd_auth01W7 ~~ wstd_eu_intW7 # Covariance
  
  # Estimate the covariances between the residuals of the within-person centered variables (the innovations).
  wstd_auth01W10 ~~ wstd_eu_intW10
  wstd_auth01W11 ~~ wstd_eu_intW11
  wstd_auth01W14 ~~ wstd_eu_intW14
  
  
  # Fix the variance and covariance of the random intercepts to 0 
  RIstd_auth01W ~~ 0*RIstd_auth01W
  RIstd_eu_intW ~~ 0*RIstd_eu_intW
  RIstd_auth01W ~~ 0*RIstd_eu_intW

  # Estimate the (residual) variance of the within-person centered variables.
  wstd_auth01W7 ~~ wstd_auth01W7 # Variances
  wstd_eu_intW7 ~~ wstd_eu_intW7 
  wstd_auth01W10 ~~ wstd_auth01W10 # Residual variances
  wstd_eu_intW10 ~~ wstd_eu_intW10 
  wstd_auth01W11 ~~ wstd_auth01W11
  wstd_eu_intW11 ~~ wstd_eu_intW11 
  wstd_auth01W14 ~~ wstd_auth01W14 
  wstd_eu_intW14 ~~ wstd_eu_intW14 
  
  # Fix error variance of indicators to (1 - alpha)*var
std_auth01W10 ~~ 0.0058*std_auth01W10
std_auth01W11 ~~ 0.002*std_auth01W11
std_auth01W14 ~~ 0.00725*std_auth01W14
std_eu_intW10 ~~ 0.0028*std_eu_intW10
std_eu_intW11 ~~ 0.00194*std_eu_intW11
std_eu_intW14 ~~ 0*std_eu_intW14


'

std_eu_attU_CLPM.fit <- lavaan(std_eu_attU_CLPM, 
                          estimator = "MLR",
                          data = att_dat,
                          group = "polatt_group",
                          missing = 'ML', 
                          meanstructure = T, 
                          int.ov.free = T) 
summary(std_eu_attU_CLPM.fit, standardized = T)

std_eu_attC_CLPM <- '
  # Create between components (random intercepts)
  RIstd_auth01W  =~ 1*std_auth01W7 + 1*std_auth01W10 + 1*std_auth01W11 + 1*std_auth01W14 
  RIstd_eu_intW =~ 1*std_eu_intW7 + 1*std_eu_intW10 + 1*std_eu_intW11 + 1*std_eu_intW14 
  
  # Create within-person centered variables
  wstd_auth01W7 =~ 1*std_auth01W7
  wstd_auth01W10 =~ 1*std_auth01W10
  wstd_auth01W11 =~ 1*std_auth01W11 
  wstd_auth01W14 =~ 1*std_auth01W14

  wstd_eu_intW7 =~ 1*std_eu_intW7
  wstd_eu_intW10 =~ 1*std_eu_intW10
  wstd_eu_intW11 =~ 1*std_eu_intW11
  wstd_eu_intW14 =~ 1*std_eu_intW14
  
  # Estimate the lagged effects between the within-person centered variables.
  wstd_auth01W10 ~ c(a1, a1)*wstd_auth01W7 + c(b1, b1)*wstd_eu_intW7
  wstd_eu_intW10 ~ c(c1, c1)*wstd_auth01W7 + c(d1, d1)*wstd_eu_intW7
  wstd_auth01W11 ~ c(a2, a2)*wstd_auth01W10 + c(b2, b2)*wstd_eu_intW10
  wstd_eu_intW11 ~ c(c2, c2)*wstd_auth01W10 + c(d2, d2)*wstd_eu_intW10
  wstd_auth01W14 ~ c(a3, a3)*wstd_auth01W11 + c(b3, b3)*wstd_eu_intW11
  wstd_eu_intW14 ~ c(c3, c3)*wstd_auth01W11 + c(d3, d3)*wstd_eu_intW11

  # Estimate the covariance between the within-person centered variables at the first wave. 
  wstd_auth01W7 ~~ wstd_eu_intW7 # Covariance
  
  # Estimate the covariances between the residuals of the within-person centered variables (the innovations).
  wstd_auth01W10 ~~ wstd_eu_intW10
  wstd_auth01W11 ~~ wstd_eu_intW11
  wstd_auth01W14 ~~ wstd_eu_intW14
  
  # Fix the variance and covariance of the random intercepts to 0 
  RIstd_auth01W ~~ 0*RIstd_auth01W
  RIstd_eu_intW ~~ 0*RIstd_eu_intW
  RIstd_auth01W ~~ 0*RIstd_eu_intW

  # Estimate the (residual) variance of the within-person centered variables.
  wstd_auth01W7 ~~ wstd_auth01W7 # Variances
  wstd_eu_intW7 ~~ wstd_eu_intW7 
  wstd_auth01W10 ~~ wstd_auth01W10 # Residual variances
  wstd_eu_intW10 ~~ wstd_eu_intW10 
  wstd_auth01W11 ~~ wstd_auth01W11
  wstd_eu_intW11 ~~ wstd_eu_intW11 
  wstd_auth01W14 ~~ wstd_auth01W14 
  wstd_eu_intW14 ~~ wstd_eu_intW14 
  
  # Fix error variance of indicators to (1 - alpha)*var
std_auth01W10 ~~ 0.0058*std_auth01W10
std_auth01W11 ~~ 0.002*std_auth01W11
std_auth01W14 ~~ 0.00725*std_auth01W14
std_eu_intW10 ~~ 0.0028*std_eu_intW10
std_eu_intW11 ~~ 0.00194*std_eu_intW11
std_eu_intW14 ~~ 0*std_eu_intW14
'

std_eu_attC_CLPM.fit <- lavaan(std_eu_attC_CLPM, 
                          estimator = "MLR",
                          data = att_dat,
                          group = "polatt_group",
                          missing = 'ML', 
                          meanstructure = T, 
                          int.ov.free = T) 
summary(std_eu_attC_CLPM.fit, standardized = T)

anova(std_eu_attC_CLPM.fit, std_eu_attU_CLPM.fit)

# High 

std_eu_CLPM_H <- '
  # Create between components (random intercepts)
  RIstd_auth01W  =~ 1*std_auth01W7 + 1*std_auth01W10 + 1*std_auth01W11 + 1*std_auth01W14 
  RIstd_eu_intW =~ 1*std_eu_intW7 + 1*std_eu_intW10 + 1*std_eu_intW11 + 1*std_eu_intW14 
  
  # Create within-person centered variables
  wstd_auth01W7 =~ NA*std_auth01W7
  wstd_auth01W10 =~ NA*std_auth01W10
  wstd_auth01W11 =~ NA*std_auth01W11 
  wstd_auth01W14 =~ NA*std_auth01W14

  wstd_eu_intW7 =~ NA*std_eu_intW7
  wstd_eu_intW10 =~ NA*std_eu_intW10
  wstd_eu_intW11 =~ NA*std_eu_intW11
  wstd_eu_intW14 =~ NA*std_eu_intW14

  # Estimate the lagged effects between the within-person centered variables.
  wstd_auth01W10 ~ a*wstd_auth01W7 + b*wstd_eu_intW7
  wstd_eu_intW10 ~ c*wstd_auth01W7 + d*wstd_eu_intW7
  wstd_auth01W11 ~ a*wstd_auth01W10 + b*wstd_eu_intW10
  wstd_eu_intW11 ~ c*wstd_auth01W10 + d*wstd_eu_intW10
  wstd_auth01W14 ~ a*wstd_auth01W11 + b*wstd_eu_intW11
  wstd_eu_intW14 ~ c*wstd_auth01W11 + d*wstd_eu_intW11

  # Estimate the covariance between the within-person centered variables at the first wave. 
  wstd_auth01W7 ~~ cor1*wstd_eu_intW7 # Covariance
  
  # Estimate the covariances between the residuals of the within-person centered variables (the innovations).
  wstd_auth01W10 ~~ rcov2*wstd_eu_intW10
  wstd_auth01W11 ~~ rcov3*wstd_eu_intW11
  wstd_auth01W14 ~~ rcov4*wstd_eu_intW14
  
  
  # Fix the variance and covariance of the random intercepts to 0 
  RIstd_auth01W ~~ 0*RIstd_auth01W
  RIstd_eu_intW ~~ 0*RIstd_eu_intW
  RIstd_auth01W ~~ 0*RIstd_eu_intW

  # Set variances of within-components at first wave to 1
  wstd_auth01W7 ~~ 1*wstd_auth01W7 # Variances
  wstd_eu_intW7 ~~ 1*wstd_eu_intW7 
  
  wstd_auth01W10 ~~ rvx2*wstd_auth01W10 # Residual variances
  wstd_eu_intW10 ~~ rvy2*wstd_eu_intW10 
  wstd_auth01W11 ~~ rvx3*wstd_auth01W11
  wstd_eu_intW11 ~~ rvy3*wstd_eu_intW11 
  wstd_auth01W14 ~~ rvx4*wstd_auth01W14 
  wstd_eu_intW14 ~~ rvy4*wstd_eu_intW14 
  
  # Fix error variance of indicators to (1 - alpha)*var
std_auth01W10 ~~ 0.0058*std_auth01W10
std_auth01W11 ~~ 0.002*std_auth01W11
std_auth01W14 ~~ 0.00725*std_auth01W14
std_eu_intW10 ~~ 0.0028*std_eu_intW10
std_eu_intW11 ~~ 0.00194*std_eu_intW11
std_eu_intW14 ~~ 0*std_eu_intW14

 # Compute correlations of within-components at each wave
  cor2 := a*c + b*d + a*d*cor1 + b*c*cor1 + rcov2
  cor3 := a*c + b*d + a*d*cor2 + b*c*cor2 + rcov3
  cor4 := a*c + b*d + a*d*cor3 + b*c*cor3 + rcov4
 
  # Contrain residual variances of within-components such that variance of each 
  # within-component equals 1
  rvx2 == 1 - (a*a + b*b + 2*a*b*cor1)
  rvy2 == 1 - (c*c + d*d + 2*c*d*cor1)
  rvx3 == 1 - (a*a + b*b + 2*a*b*cor2)
  rvy3 == 1 - (c*c + d*d + 2*c*d*cor2)
  rvx4 == 1 - (a*a + b*b + 2*a*b*cor3)
  rvy4 == 1 - (c*c + d*d + 2*c*d*cor3)
'

std_eu_CLPM_H.fit <- lavaan(std_eu_CLPM_H, 
                               estimator = "MLR",
                               data = att_dat_high,
                               missing = 'ML', 
                               meanstructure = T, 
                               int.ov.free = T) 
summary(std_eu_CLPM_H.fit, standardized = T)

std_eu_CLPM_H_noEQ <- '
  # Create between components (random intercepts)
  RIstd_auth01W  =~ 1*std_auth01W7 + 1*std_auth01W10 + 1*std_auth01W11 + 1*std_auth01W14 
  RIstd_eu_intW =~ 1*std_eu_intW7 + 1*std_eu_intW10 + 1*std_eu_intW11 + 1*std_eu_intW14 
  
  # Create within-person centered variables
  wstd_auth01W7 =~ 1*std_auth01W7
  wstd_auth01W10 =~ 1*std_auth01W10
  wstd_auth01W11 =~ 1*std_auth01W11 
  wstd_auth01W14 =~ 1*std_auth01W14

  wstd_eu_intW7 =~ 1*std_eu_intW7
  wstd_eu_intW10 =~ 1*std_eu_intW10
  wstd_eu_intW11 =~ 1*std_eu_intW11
  wstd_eu_intW14 =~ 1*std_eu_intW14

  # Estimate the lagged effects between the within-person centered variables.
  wstd_auth01W10 + wstd_eu_intW10 ~ wstd_auth01W7 + wstd_eu_intW7
  wstd_auth01W11 + wstd_eu_intW11 ~ wstd_auth01W10 + wstd_eu_intW10
  wstd_auth01W14 + wstd_eu_intW14 ~ wstd_auth01W11 + wstd_eu_intW11

  # Estimate the covariance between the within-person centered variables at the first wave. 
  wstd_auth01W7 ~~ wstd_eu_intW7 # Covariance
  
  # Estimate the covariances between the residuals of the within-person centered variables (the innovations).
  wstd_auth01W10 ~~ wstd_eu_intW10
  wstd_auth01W11 ~~ wstd_eu_intW11
  wstd_auth01W14 ~~ wstd_eu_intW14
  
  
  # Fix the variance and covariance of the random intercepts to 0 
  RIstd_auth01W ~~ 0*RIstd_auth01W
  RIstd_eu_intW ~~ 0*RIstd_eu_intW
  RIstd_auth01W ~~ 0*RIstd_eu_intW

  # Estimate the (residual) variance of the within-person centered variables.
  wstd_auth01W7 ~~ wstd_auth01W7 # Variances
  wstd_eu_intW7 ~~ wstd_eu_intW7 
  wstd_auth01W10 ~~ wstd_auth01W10 # Residual variances
  wstd_eu_intW10 ~~ wstd_eu_intW10 
  wstd_auth01W11 ~~ wstd_auth01W11
  wstd_eu_intW11 ~~ wstd_eu_intW11 
  wstd_auth01W14 ~~ wstd_auth01W14 
  wstd_eu_intW14 ~~ wstd_eu_intW14 
  
  # Fix error variance of indicators to (1 - alpha)*var
std_auth01W10 ~~ 0.0058*std_auth01W10
std_auth01W11 ~~ 0.002*std_auth01W11
std_auth01W14 ~~ 0.00725*std_auth01W14
std_eu_intW10 ~~ 0.0028*std_eu_intW10
std_eu_intW11 ~~ 0.00194*std_eu_intW11
std_eu_intW14 ~~ 0*std_eu_intW14


'

std_eu_CLPM_H_noEQ.fit <- lavaan(std_eu_CLPM_H_noEQ, 
                            estimator = "MLR",
                            data = att_dat_high,
                            missing = 'ML', 
                            meanstructure = T, 
                            int.ov.free = T) 
summary(std_eu_CLPM_H_noEQ.fit, standardized = T)

# Low

std_eu_CLPM_L <- '
  # Create between components (random intercepts)
  RIstd_auth01W  =~ 1*std_auth01W7 + 1*std_auth01W10 + 1*std_auth01W11 + 1*std_auth01W14 
  RIstd_eu_intW =~ 1*std_eu_intW7 + 1*std_eu_intW10 + 1*std_eu_intW11 + 1*std_eu_intW14 
  
  # Create within-person centered variables
  wstd_auth01W7 =~ NA*std_auth01W7
  wstd_auth01W10 =~ NA*std_auth01W10
  wstd_auth01W11 =~ NA*std_auth01W11 
  wstd_auth01W14 =~ NA*std_auth01W14

  wstd_eu_intW7 =~ NA*std_eu_intW7
  wstd_eu_intW10 =~ NA*std_eu_intW10
  wstd_eu_intW11 =~ NA*std_eu_intW11
  wstd_eu_intW14 =~ NA*std_eu_intW14

  # Estimate the lagged effects between the within-person centered variables.
  wstd_auth01W10 ~ a*wstd_auth01W7 + b*wstd_eu_intW7
  wstd_eu_intW10 ~ c*wstd_auth01W7 + d*wstd_eu_intW7
  wstd_auth01W11 ~ a*wstd_auth01W10 + b*wstd_eu_intW10
  wstd_eu_intW11 ~ c*wstd_auth01W10 + d*wstd_eu_intW10
  wstd_auth01W14 ~ a*wstd_auth01W11 + b*wstd_eu_intW11
  wstd_eu_intW14 ~ c*wstd_auth01W11 + d*wstd_eu_intW11

  # Estimate the covariance between the within-person centered variables at the first wave. 
  wstd_auth01W7 ~~ cor1*wstd_eu_intW7 # Covariance
  
  # Estimate the covariances between the residuals of the within-person centered variables (the innovations).
  wstd_auth01W10 ~~ rcov2*wstd_eu_intW10
  wstd_auth01W11 ~~ rcov3*wstd_eu_intW11
  wstd_auth01W14 ~~ rcov4*wstd_eu_intW14
  
  
  # Fix the variance and covariance of the random intercepts to 0 
  RIstd_auth01W ~~ 0*RIstd_auth01W
  RIstd_eu_intW ~~ 0*RIstd_eu_intW
  RIstd_auth01W ~~ 0*RIstd_eu_intW

  # Set variances of within-components at first wave to 1
  wstd_auth01W7 ~~ 1*wstd_auth01W7 # Variances
  wstd_eu_intW7 ~~ 1*wstd_eu_intW7 
  
  wstd_auth01W10 ~~ rvx2*wstd_auth01W10 # Residual variances
  wstd_eu_intW10 ~~ rvy2*wstd_eu_intW10 
  wstd_auth01W11 ~~ rvx3*wstd_auth01W11
  wstd_eu_intW11 ~~ rvy3*wstd_eu_intW11 
  wstd_auth01W14 ~~ rvx4*wstd_auth01W14 
  wstd_eu_intW14 ~~ rvy4*wstd_eu_intW14 
  
  # Fix error variance of indicators to (1 - alpha)*var
std_auth01W10 ~~ 0.0058*std_auth01W10
std_auth01W11 ~~ 0.002*std_auth01W11
std_auth01W14 ~~ 0.00725*std_auth01W14
std_eu_intW10 ~~ 0.0028*std_eu_intW10
std_eu_intW11 ~~ 0.00194*std_eu_intW11
std_eu_intW14 ~~ 0*std_eu_intW14

 # Compute correlations of within-components at each wave
  cor2 := a*c + b*d + a*d*cor1 + b*c*cor1 + rcov2
  cor3 := a*c + b*d + a*d*cor2 + b*c*cor2 + rcov3
  cor4 := a*c + b*d + a*d*cor3 + b*c*cor3 + rcov4
 
  # Contrain residual variances of within-components such that variance of each 
  # within-component equals 1
  rvx2 == 1 - (a*a + b*b + 2*a*b*cor1)
  rvy2 == 1 - (c*c + d*d + 2*c*d*cor1)
  rvx3 == 1 - (a*a + b*b + 2*a*b*cor2)
  rvy3 == 1 - (c*c + d*d + 2*c*d*cor2)
  rvx4 == 1 - (a*a + b*b + 2*a*b*cor3)
  rvy4 == 1 - (c*c + d*d + 2*c*d*cor3)
'

std_eu_CLPM_L.fit <- lavaan(std_eu_CLPM_L, 
                            estimator = "MLR",
                            data = att_dat_low,
                            missing = 'ML', 
                            meanstructure = T, 
                            int.ov.free = T) 
summary(std_eu_CLPM_L.fit, standardized = T)

std_eu_CLPM_L_noEQ <- '
  # Create between components (random intercepts)
  RIstd_auth01W  =~ 1*std_auth01W7 + 1*std_auth01W10 + 1*std_auth01W11 + 1*std_auth01W14 
  RIstd_eu_intW =~ 1*std_eu_intW7 + 1*std_eu_intW10 + 1*std_eu_intW11 + 1*std_eu_intW14 
  
  # Create within-person centered variables
  wstd_auth01W7 =~ 1*std_auth01W7
  wstd_auth01W10 =~ 1*std_auth01W10
  wstd_auth01W11 =~ 1*std_auth01W11 
  wstd_auth01W14 =~ 1*std_auth01W14

  wstd_eu_intW7 =~ 1*std_eu_intW7
  wstd_eu_intW10 =~ 1*std_eu_intW10
  wstd_eu_intW11 =~ 1*std_eu_intW11
  wstd_eu_intW14 =~ 1*std_eu_intW14

  # Estimate the lagged effects between the within-person centered variables.
  wstd_auth01W10 + wstd_eu_intW10 ~ wstd_auth01W7 + wstd_eu_intW7
  wstd_auth01W11 + wstd_eu_intW11 ~ wstd_auth01W10 + wstd_eu_intW10
  wstd_auth01W14 + wstd_eu_intW14 ~ wstd_auth01W11 + wstd_eu_intW11

  # Estimate the covariance between the within-person centered variables at the first wave. 
  wstd_auth01W7 ~~ wstd_eu_intW7 # Covariance
  
  # Estimate the covariances between the residuals of the within-person centered variables (the innovations).
  wstd_auth01W10 ~~ wstd_eu_intW10
  wstd_auth01W11 ~~ wstd_eu_intW11
  wstd_auth01W14 ~~ wstd_eu_intW14
  
  
  # Fix the variance and covariance of the random intercepts to 0 
  RIstd_auth01W ~~ 0*RIstd_auth01W
  RIstd_eu_intW ~~ 0*RIstd_eu_intW
  RIstd_auth01W ~~ 0*RIstd_eu_intW

  # Estimate the (residual) variance of the within-person centered variables.
  wstd_auth01W7 ~~ wstd_auth01W7 # Variances
  wstd_eu_intW7 ~~ wstd_eu_intW7 
  wstd_auth01W10 ~~ wstd_auth01W10 # Residual variances
  wstd_eu_intW10 ~~ wstd_eu_intW10 
  wstd_auth01W11 ~~ wstd_auth01W11
  wstd_eu_intW11 ~~ wstd_eu_intW11 
  wstd_auth01W14 ~~ wstd_auth01W14 
  wstd_eu_intW14 ~~ wstd_eu_intW14 
  
  # Fix error variance of indicators to (1 - alpha)*var
std_auth01W10 ~~ 0.0058*std_auth01W10
std_auth01W11 ~~ 0.002*std_auth01W11
std_auth01W14 ~~ 0.00725*std_auth01W14
std_eu_intW10 ~~ 0.0028*std_eu_intW10
std_eu_intW11 ~~ 0.00194*std_eu_intW11
std_eu_intW14 ~~ 0*std_eu_intW14


'

std_eu_CLPM_L_noEQ.fit <- lavaan(std_eu_CLPM_L_noEQ, 
                                 estimator = "MLR",
                                 data = att_dat_low,
                                 missing = 'ML', 
                                 meanstructure = T, 
                                 int.ov.free = T) 
summary(std_eu_CLPM_L_noEQ.fit, standardized = T)

# Fit measures:

fitmeasures(std_eu_CLPM_H.fit)
fitmeasures(std_eu_CLPM_H_noEQ.fit)
fitmeasures(std_eu_CLPM_L.fit)
fitmeasures(std_eu_CLPM_L_noEQ.fit)

# Results 

standardizedsolution(std_eu_CLPM_H.fit)
standardizedsolution(std_eu_CLPM_H_noEQ.fit)
standardizedsolution(std_eu_CLPM_L.fit)
standardizedsolution(std_eu_CLPM_L_noEQ.fit)

## Covariate models:

std_eu_CLPM_H_tvc <- '
  # Create between components (random intercepts)
  RIstd_auth01W  =~ 1*std_auth01W7 + 1*std_auth01W10 + 1*std_auth01W11 + 1*std_auth01W14 
  RIstd_eu_intW =~ 1*std_eu_intW7 + 1*std_eu_intW10 + 1*std_eu_intW11 + 1*std_eu_intW14 
  
  # Create within-person centered variables
  wstd_auth01W7 =~ NA*std_auth01W7
  wstd_auth01W10 =~ NA*std_auth01W10
  wstd_auth01W11 =~ NA*std_auth01W11 
  wstd_auth01W14 =~ NA*std_auth01W14

  wstd_eu_intW7 =~ NA*std_eu_intW7
  wstd_eu_intW10 =~ NA*std_eu_intW10
  wstd_eu_intW11 =~ NA*std_eu_intW11
  wstd_eu_intW14 =~ NA*std_eu_intW14

  # Estimate the lagged effects between the within-person centered variables.
  wstd_auth01W10 ~ a*wstd_auth01W7 + b*wstd_eu_intW7
  wstd_eu_intW10 ~ c*wstd_auth01W7 + d*wstd_eu_intW7
  wstd_auth01W11 ~ a*wstd_auth01W10 + b*wstd_eu_intW10
  wstd_eu_intW11 ~ c*wstd_auth01W10 + d*wstd_eu_intW10
  wstd_auth01W14 ~ a*wstd_auth01W11 + b*wstd_eu_intW11
  wstd_eu_intW14 ~ c*wstd_auth01W11 + d*wstd_eu_intW11

  # Estimate the covariance between the within-person centered variables at the first wave. 
  wstd_auth01W7 ~~ cor1*wstd_eu_intW7 # Covariance
  
  # Estimate the covariances between the residuals of the within-person centered variables (the innovations).
  wstd_auth01W10 ~~ rcov2*wstd_eu_intW10
  wstd_auth01W11 ~~ rcov3*wstd_eu_intW11
  wstd_auth01W14 ~~ rcov4*wstd_eu_intW14
  
  
  # Fix the variance and covariance of the random intercepts to 0 
  RIstd_auth01W ~~ 0*RIstd_auth01W
  RIstd_eu_intW ~~ 0*RIstd_eu_intW
  RIstd_auth01W ~~ 0*RIstd_eu_intW

  # Set variances of within-components at first wave to 1
  wstd_auth01W7 ~~ 1*wstd_auth01W7 # Variances
  wstd_eu_intW7 ~~ 1*wstd_eu_intW7 
  
  wstd_auth01W10 ~~ rvx2*wstd_auth01W10 # Residual variances
  wstd_eu_intW10 ~~ rvy2*wstd_eu_intW10 
  wstd_auth01W11 ~~ rvx3*wstd_auth01W11
  wstd_eu_intW11 ~~ rvy3*wstd_eu_intW11 
  wstd_auth01W14 ~~ rvx4*wstd_auth01W14 
  wstd_eu_intW14 ~~ rvy4*wstd_eu_intW14 
  
  # Fix error variance of indicators to (1 - alpha)*var
std_auth01W10 ~~ 0.0058*std_auth01W10
std_auth01W11 ~~ 0.002*std_auth01W11
std_auth01W14 ~~ 0.00725*std_auth01W14
std_eu_intW10 ~~ 0.0028*std_eu_intW10
std_eu_intW11 ~~ 0.00194*std_eu_intW11
std_eu_intW14 ~~ 0*std_eu_intW14

 # Compute correlations of within-components at each wave
  cor2 := a*c + b*d + a*d*cor1 + b*c*cor1 + rcov2
  cor3 := a*c + b*d + a*d*cor2 + b*c*cor2 + rcov3
  cor4 := a*c + b*d + a*d*cor3 + b*c*cor3 + rcov4
 
  # Contrain residual variances of within-components such that variance of each 
  # within-component equals 1
  rvx2 == 1 - (a*a + b*b + 2*a*b*cor1)
  rvy2 == 1 - (c*c + d*d + 2*c*d*cor1)
  rvx3 == 1 - (a*a + b*b + 2*a*b*cor2)
  rvy3 == 1 - (c*c + d*d + 2*c*d*cor2)
  rvx4 == 1 - (a*a + b*b + 2*a*b*cor3)
  rvy4 == 1 - (c*c + d*d + 2*c*d*cor3)
  
  # incorporate tvc - age
wstd_auth01W7 + wstd_eu_intW7 ~ ageW7
wstd_auth01W10 + wstd_eu_intW10 ~ ageW10
wstd_auth01W11 + wstd_eu_intW11 ~ ageW11
wstd_auth01W14 + wstd_eu_intW14 ~ ageW14

ageW7 ~~ ageW7
ageW10 ~~ ageW10
ageW11 ~~ ageW11
ageW14 ~~ ageW14

ageW7 ~~ ageW10 + ageW11 + ageW14
ageW10 ~~ ageW11 + ageW14
ageW11 ~~ ageW14

# incorporate tvc - ethnicity
wstd_auth01W7 + wstd_eu_intW7 ~ ethnic7
wstd_auth01W10 + wstd_eu_intW10 ~ ethnic10
wstd_auth01W11 + wstd_eu_intW11 ~ ethnic11
wstd_auth01W14 + wstd_eu_intW14 ~ ethnic14

ethnic7 ~~ ethnic7
ethnic10 ~~ ethnic10
ethnic11 ~~ ethnic11
ethnic14 ~~ ethnic14

ethnic7 ~~ ethnic10 + ethnic11 + ethnic14
ethnic10 ~~ ethnic11 + ethnic14
ethnic11 ~~ ethnic14

# incorporate tvc – social grade
wstd_auth01W7 + wstd_eu_intW7 ~ p_socgradeW7
wstd_auth01W10 + wstd_eu_intW10 ~ p_socgradeW10
wstd_auth01W11 + wstd_eu_intW11 ~ p_socgradeW11
wstd_auth01W14 + wstd_eu_intW14 ~ p_socgradeW14

p_socgradeW7 ~~ p_socgradeW7
p_socgradeW10 ~~ p_socgradeW10
p_socgradeW11 ~~ p_socgradeW11
p_socgradeW14 ~~ p_socgradeW14

p_socgradeW7 ~~ p_socgradeW10 + p_socgradeW11 + p_socgradeW14
p_socgradeW10 ~~ p_socgradeW11 + p_socgradeW14
p_socgradeW11 ~~ p_socgradeW14

# incorporate tvc – education
wstd_auth01W7 + wstd_eu_intW7 ~ higheduc7
wstd_auth01W10 + wstd_eu_intW10 ~ higheduc10
wstd_auth01W11 + wstd_eu_intW11 ~ higheduc11
wstd_auth01W14 + wstd_eu_intW14 ~ higheduc14

higheduc7 ~~ higheduc7
higheduc10 ~~ higheduc10
higheduc11 ~~ higheduc11
higheduc14 ~~ higheduc14

higheduc7 ~~ higheduc10 + higheduc11 + higheduc14
higheduc10 ~~ higheduc11 + higheduc14
higheduc11 ~~ higheduc14

# incorporate tvc – income
wstd_auth01W7 + wstd_eu_intW7 ~ incomerec7
wstd_auth01W10 + wstd_eu_intW10 ~ incomerec10
wstd_auth01W11 + wstd_eu_intW11 ~ incomerec11
wstd_auth01W14 + wstd_eu_intW14 ~ incomerec14

incomerec7 ~~ incomerec7
incomerec10 ~~ incomerec10
incomerec11 ~~ incomerec11
incomerec14 ~~ incomerec14

incomerec7 ~~ incomerec10 + incomerec11 + incomerec14
incomerec10 ~~ incomerec11 + incomerec14
incomerec11 ~~ incomerec14
'

std_eu_CLPM_H_tvc.fit <- lavaan(std_eu_CLPM_H_tvc, 
                            estimator = "MLR",
                            data = att_dat_high,
                            missing = 'ML', 
                            meanstructure = T, 
                            int.ov.free = T) 
summary(std_eu_CLPM_H_tvc.fit, standardized = T)

std_eu_CLPM_H_noEQ_tvc <- '
  # Create between components (random intercepts)
  RIstd_auth01W  =~ 1*std_auth01W7 + 1*std_auth01W10 + 1*std_auth01W11 + 1*std_auth01W14 
  RIstd_eu_intW =~ 1*std_eu_intW7 + 1*std_eu_intW10 + 1*std_eu_intW11 + 1*std_eu_intW14 
  
  # Create within-person centered variables
  wstd_auth01W7 =~ 1*std_auth01W7
  wstd_auth01W10 =~ 1*std_auth01W10
  wstd_auth01W11 =~ 1*std_auth01W11 
  wstd_auth01W14 =~ 1*std_auth01W14

  wstd_eu_intW7 =~ 1*std_eu_intW7
  wstd_eu_intW10 =~ 1*std_eu_intW10
  wstd_eu_intW11 =~ 1*std_eu_intW11
  wstd_eu_intW14 =~ 1*std_eu_intW14

  # Estimate the lagged effects between the within-person centered variables.
  wstd_auth01W10 + wstd_eu_intW10 ~ wstd_auth01W7 + wstd_eu_intW7
  wstd_auth01W11 + wstd_eu_intW11 ~ wstd_auth01W10 + wstd_eu_intW10
  wstd_auth01W14 + wstd_eu_intW14 ~ wstd_auth01W11 + wstd_eu_intW11

  # Estimate the covariance between the within-person centered variables at the first wave. 
  wstd_auth01W7 ~~ wstd_eu_intW7 # Covariance
  
  # Estimate the covariances between the residuals of the within-person centered variables (the innovations).
  wstd_auth01W10 ~~ wstd_eu_intW10
  wstd_auth01W11 ~~ wstd_eu_intW11
  wstd_auth01W14 ~~ wstd_eu_intW14
  
  
  # Fix the variance and covariance of the random intercepts to 0 
  RIstd_auth01W ~~ 0*RIstd_auth01W
  RIstd_eu_intW ~~ 0*RIstd_eu_intW
  RIstd_auth01W ~~ 0*RIstd_eu_intW

  # Estimate the (residual) variance of the within-person centered variables.
  wstd_auth01W7 ~~ wstd_auth01W7 # Variances
  wstd_eu_intW7 ~~ wstd_eu_intW7 
  wstd_auth01W10 ~~ wstd_auth01W10 # Residual variances
  wstd_eu_intW10 ~~ wstd_eu_intW10 
  wstd_auth01W11 ~~ wstd_auth01W11
  wstd_eu_intW11 ~~ wstd_eu_intW11 
  wstd_auth01W14 ~~ wstd_auth01W14 
  wstd_eu_intW14 ~~ wstd_eu_intW14 
  
  # Fix error variance of indicators to (1 - alpha)*var
std_auth01W10 ~~ 0.0058*std_auth01W10
std_auth01W11 ~~ 0.002*std_auth01W11
std_auth01W14 ~~ 0.00725*std_auth01W14
std_eu_intW10 ~~ 0.0028*std_eu_intW10
std_eu_intW11 ~~ 0.00194*std_eu_intW11
std_eu_intW14 ~~ 0*std_eu_intW14

  # incorporate tvc - age
wstd_auth01W7 + wstd_eu_intW7 ~ ageW7
wstd_auth01W10 + wstd_eu_intW10 ~ ageW10
wstd_auth01W11 + wstd_eu_intW11 ~ ageW11
wstd_auth01W14 + wstd_eu_intW14 ~ ageW14

ageW7 ~~ ageW7
ageW10 ~~ ageW10
ageW11 ~~ ageW11
ageW14 ~~ ageW14

ageW7 ~~ ageW10 + ageW11 + ageW14
ageW10 ~~ ageW11 + ageW14
ageW11 ~~ ageW14

# incorporate tvc - ethnicity
wstd_auth01W7 + wstd_eu_intW7 ~ ethnic7
wstd_auth01W10 + wstd_eu_intW10 ~ ethnic10
wstd_auth01W11 + wstd_eu_intW11 ~ ethnic11
wstd_auth01W14 + wstd_eu_intW14 ~ ethnic14

ethnic7 ~~ ethnic7
ethnic10 ~~ ethnic10
ethnic11 ~~ ethnic11
ethnic14 ~~ ethnic14

ethnic7 ~~ ethnic10 + ethnic11 + ethnic14
ethnic10 ~~ ethnic11 + ethnic14
ethnic11 ~~ ethnic14

# incorporate tvc – social grade
wstd_auth01W7 + wstd_eu_intW7 ~ p_socgradeW7
wstd_auth01W10 + wstd_eu_intW10 ~ p_socgradeW10
wstd_auth01W11 + wstd_eu_intW11 ~ p_socgradeW11
wstd_auth01W14 + wstd_eu_intW14 ~ p_socgradeW14

p_socgradeW7 ~~ p_socgradeW7
p_socgradeW10 ~~ p_socgradeW10
p_socgradeW11 ~~ p_socgradeW11
p_socgradeW14 ~~ p_socgradeW14

p_socgradeW7 ~~ p_socgradeW10 + p_socgradeW11 + p_socgradeW14
p_socgradeW10 ~~ p_socgradeW11 + p_socgradeW14
p_socgradeW11 ~~ p_socgradeW14

# incorporate tvc – education
wstd_auth01W7 + wstd_eu_intW7 ~ higheduc7
wstd_auth01W10 + wstd_eu_intW10 ~ higheduc10
wstd_auth01W11 + wstd_eu_intW11 ~ higheduc11
wstd_auth01W14 + wstd_eu_intW14 ~ higheduc14

higheduc7 ~~ higheduc7
higheduc10 ~~ higheduc10
higheduc11 ~~ higheduc11
higheduc14 ~~ higheduc14

higheduc7 ~~ higheduc10 + higheduc11 + higheduc14
higheduc10 ~~ higheduc11 + higheduc14
higheduc11 ~~ higheduc14

# incorporate tvc – income
wstd_auth01W7 + wstd_eu_intW7 ~ incomerec7
wstd_auth01W10 + wstd_eu_intW10 ~ incomerec10
wstd_auth01W11 + wstd_eu_intW11 ~ incomerec11
wstd_auth01W14 + wstd_eu_intW14 ~ incomerec14

incomerec7 ~~ incomerec7
incomerec10 ~~ incomerec10
incomerec11 ~~ incomerec11
incomerec14 ~~ incomerec14

incomerec7 ~~ incomerec10 + incomerec11 + incomerec14
incomerec10 ~~ incomerec11 + incomerec14
incomerec11 ~~ incomerec14
'

std_eu_CLPM_H_noEQ_tvc.fit <- lavaan(std_eu_CLPM_H_noEQ_tvc, 
                                 estimator = "MLR",
                                 data = att_dat_high,
                                 missing = 'ML', 
                                 meanstructure = T, 
                                 int.ov.free = T) 
summary(std_eu_CLPM_H_noEQ_tvc.fit, standardized = T)

# Low

std_eu_CLPM_L_tvc <- '
  # Create between components (random intercepts)
  RIstd_auth01W  =~ 1*std_auth01W7 + 1*std_auth01W10 + 1*std_auth01W11 + 1*std_auth01W14 
  RIstd_eu_intW =~ 1*std_eu_intW7 + 1*std_eu_intW10 + 1*std_eu_intW11 + 1*std_eu_intW14 
  
  # Create within-person centered variables
  wstd_auth01W7 =~ NA*std_auth01W7
  wstd_auth01W10 =~ NA*std_auth01W10
  wstd_auth01W11 =~ NA*std_auth01W11 
  wstd_auth01W14 =~ NA*std_auth01W14

  wstd_eu_intW7 =~ NA*std_eu_intW7
  wstd_eu_intW10 =~ NA*std_eu_intW10
  wstd_eu_intW11 =~ NA*std_eu_intW11
  wstd_eu_intW14 =~ NA*std_eu_intW14

  # Estimate the lagged effects between the within-person centered variables.
  wstd_auth01W10 ~ a*wstd_auth01W7 + b*wstd_eu_intW7
  wstd_eu_intW10 ~ c*wstd_auth01W7 + d*wstd_eu_intW7
  wstd_auth01W11 ~ a*wstd_auth01W10 + b*wstd_eu_intW10
  wstd_eu_intW11 ~ c*wstd_auth01W10 + d*wstd_eu_intW10
  wstd_auth01W14 ~ a*wstd_auth01W11 + b*wstd_eu_intW11
  wstd_eu_intW14 ~ c*wstd_auth01W11 + d*wstd_eu_intW11

  # Estimate the covariance between the within-person centered variables at the first wave. 
  wstd_auth01W7 ~~ cor1*wstd_eu_intW7 # Covariance
  
  # Estimate the covariances between the residuals of the within-person centered variables (the innovations).
  wstd_auth01W10 ~~ rcov2*wstd_eu_intW10
  wstd_auth01W11 ~~ rcov3*wstd_eu_intW11
  wstd_auth01W14 ~~ rcov4*wstd_eu_intW14
  
  
  # Fix the variance and covariance of the random intercepts to 0 
  RIstd_auth01W ~~ 0*RIstd_auth01W
  RIstd_eu_intW ~~ 0*RIstd_eu_intW
  RIstd_auth01W ~~ 0*RIstd_eu_intW

  # Set variances of within-components at first wave to 1
  wstd_auth01W7 ~~ 1*wstd_auth01W7 # Variances
  wstd_eu_intW7 ~~ 1*wstd_eu_intW7 
  
  wstd_auth01W10 ~~ rvx2*wstd_auth01W10 # Residual variances
  wstd_eu_intW10 ~~ rvy2*wstd_eu_intW10 
  wstd_auth01W11 ~~ rvx3*wstd_auth01W11
  wstd_eu_intW11 ~~ rvy3*wstd_eu_intW11 
  wstd_auth01W14 ~~ rvx4*wstd_auth01W14 
  wstd_eu_intW14 ~~ rvy4*wstd_eu_intW14 
  
  # Fix error variance of indicators to (1 - alpha)*var
std_auth01W10 ~~ 0.0058*std_auth01W10
std_auth01W11 ~~ 0.002*std_auth01W11
std_auth01W14 ~~ 0.00725*std_auth01W14
std_eu_intW10 ~~ 0.0028*std_eu_intW10
std_eu_intW11 ~~ 0.00194*std_eu_intW11
std_eu_intW14 ~~ 0*std_eu_intW14

 # Compute correlations of within-components at each wave
  cor2 := a*c + b*d + a*d*cor1 + b*c*cor1 + rcov2
  cor3 := a*c + b*d + a*d*cor2 + b*c*cor2 + rcov3
  cor4 := a*c + b*d + a*d*cor3 + b*c*cor3 + rcov4
 
  # Contrain residual variances of within-components such that variance of each 
  # within-component equals 1
  rvx2 == 1 - (a*a + b*b + 2*a*b*cor1)
  rvy2 == 1 - (c*c + d*d + 2*c*d*cor1)
  rvx3 == 1 - (a*a + b*b + 2*a*b*cor2)
  rvy3 == 1 - (c*c + d*d + 2*c*d*cor2)
  rvx4 == 1 - (a*a + b*b + 2*a*b*cor3)
  rvy4 == 1 - (c*c + d*d + 2*c*d*cor3)
  
    # incorporate tvc - age
wstd_auth01W7 + wstd_eu_intW7 ~ ageW7
wstd_auth01W10 + wstd_eu_intW10 ~ ageW10
wstd_auth01W11 + wstd_eu_intW11 ~ ageW11
wstd_auth01W14 + wstd_eu_intW14 ~ ageW14

ageW7 ~~ ageW7
ageW10 ~~ ageW10
ageW11 ~~ ageW11
ageW14 ~~ ageW14

ageW7 ~~ ageW10 + ageW11 + ageW14
ageW10 ~~ ageW11 + ageW14
ageW11 ~~ ageW14

# incorporate tvc - ethnicity
wstd_auth01W7 + wstd_eu_intW7 ~ ethnic7
wstd_auth01W10 + wstd_eu_intW10 ~ ethnic10
wstd_auth01W11 + wstd_eu_intW11 ~ ethnic11
wstd_auth01W14 + wstd_eu_intW14 ~ ethnic14

ethnic7 ~~ ethnic7
ethnic10 ~~ ethnic10
ethnic11 ~~ ethnic11
ethnic14 ~~ ethnic14

ethnic7 ~~ ethnic10 + ethnic11 + ethnic14
ethnic10 ~~ ethnic11 + ethnic14
ethnic11 ~~ ethnic14

# incorporate tvc – social grade
wstd_auth01W7 + wstd_eu_intW7 ~ p_socgradeW7
wstd_auth01W10 + wstd_eu_intW10 ~ p_socgradeW10
wstd_auth01W11 + wstd_eu_intW11 ~ p_socgradeW11
wstd_auth01W14 + wstd_eu_intW14 ~ p_socgradeW14

p_socgradeW7 ~~ p_socgradeW7
p_socgradeW10 ~~ p_socgradeW10
p_socgradeW11 ~~ p_socgradeW11
p_socgradeW14 ~~ p_socgradeW14

p_socgradeW7 ~~ p_socgradeW10 + p_socgradeW11 + p_socgradeW14
p_socgradeW10 ~~ p_socgradeW11 + p_socgradeW14
p_socgradeW11 ~~ p_socgradeW14

# incorporate tvc – education
wstd_auth01W7 + wstd_eu_intW7 ~ higheduc7
wstd_auth01W10 + wstd_eu_intW10 ~ higheduc10
wstd_auth01W11 + wstd_eu_intW11 ~ higheduc11
wstd_auth01W14 + wstd_eu_intW14 ~ higheduc14

higheduc7 ~~ higheduc7
higheduc10 ~~ higheduc10
higheduc11 ~~ higheduc11
higheduc14 ~~ higheduc14

higheduc7 ~~ higheduc10 + higheduc11 + higheduc14
higheduc10 ~~ higheduc11 + higheduc14
higheduc11 ~~ higheduc14

# incorporate tvc – income
wstd_auth01W7 + wstd_eu_intW7 ~ incomerec7
wstd_auth01W10 + wstd_eu_intW10 ~ incomerec10
wstd_auth01W11 + wstd_eu_intW11 ~ incomerec11
wstd_auth01W14 + wstd_eu_intW14 ~ incomerec14

incomerec7 ~~ incomerec7
incomerec10 ~~ incomerec10
incomerec11 ~~ incomerec11
incomerec14 ~~ incomerec14

incomerec7 ~~ incomerec10 + incomerec11 + incomerec14
incomerec10 ~~ incomerec11 + incomerec14
incomerec11 ~~ incomerec14
'

std_eu_CLPM_L_tvc.fit <- lavaan(std_eu_CLPM_L_tvc, 
                            estimator = "MLR",
                            data = att_dat_low,
                            missing = 'ML', 
                            meanstructure = T, 
                            int.ov.free = T) 
summary(std_eu_CLPM_L_tvc.fit, standardized = T)

std_eu_CLPM_L_noEQ_tvc <- '
  # Create between components (random intercepts)
  RIstd_auth01W  =~ 1*std_auth01W7 + 1*std_auth01W10 + 1*std_auth01W11 + 1*std_auth01W14 
  RIstd_eu_intW =~ 1*std_eu_intW7 + 1*std_eu_intW10 + 1*std_eu_intW11 + 1*std_eu_intW14 
  
  # Create within-person centered variables
  wstd_auth01W7 =~ 1*std_auth01W7
  wstd_auth01W10 =~ 1*std_auth01W10
  wstd_auth01W11 =~ 1*std_auth01W11 
  wstd_auth01W14 =~ 1*std_auth01W14

  wstd_eu_intW7 =~ 1*std_eu_intW7
  wstd_eu_intW10 =~ 1*std_eu_intW10
  wstd_eu_intW11 =~ 1*std_eu_intW11
  wstd_eu_intW14 =~ 1*std_eu_intW14

  # Estimate the lagged effects between the within-person centered variables.
  wstd_auth01W10 + wstd_eu_intW10 ~ wstd_auth01W7 + wstd_eu_intW7
  wstd_auth01W11 + wstd_eu_intW11 ~ wstd_auth01W10 + wstd_eu_intW10
  wstd_auth01W14 + wstd_eu_intW14 ~ wstd_auth01W11 + wstd_eu_intW11

  # Estimate the covariance between the within-person centered variables at the first wave. 
  wstd_auth01W7 ~~ wstd_eu_intW7 # Covariance
  
  # Estimate the covariances between the residuals of the within-person centered variables (the innovations).
  wstd_auth01W10 ~~ wstd_eu_intW10
  wstd_auth01W11 ~~ wstd_eu_intW11
  wstd_auth01W14 ~~ wstd_eu_intW14
  
  
  # Fix the variance and covariance of the random intercepts to 0 
  RIstd_auth01W ~~ 0*RIstd_auth01W
  RIstd_eu_intW ~~ 0*RIstd_eu_intW
  RIstd_auth01W ~~ 0*RIstd_eu_intW

  # Estimate the (residual) variance of the within-person centered variables.
  wstd_auth01W7 ~~ wstd_auth01W7 # Variances
  wstd_eu_intW7 ~~ wstd_eu_intW7 
  wstd_auth01W10 ~~ wstd_auth01W10 # Residual variances
  wstd_eu_intW10 ~~ wstd_eu_intW10 
  wstd_auth01W11 ~~ wstd_auth01W11
  wstd_eu_intW11 ~~ wstd_eu_intW11 
  wstd_auth01W14 ~~ wstd_auth01W14 
  wstd_eu_intW14 ~~ wstd_eu_intW14 
  
  # Fix error variance of indicators to (1 - alpha)*var
std_auth01W10 ~~ 0.0058*std_auth01W10
std_auth01W11 ~~ 0.002*std_auth01W11
std_auth01W14 ~~ 0.00725*std_auth01W14
std_eu_intW10 ~~ 0.0028*std_eu_intW10
std_eu_intW11 ~~ 0.00194*std_eu_intW11
std_eu_intW14 ~~ 0*std_eu_intW14

  # incorporate tvc - age
wstd_auth01W7 + wstd_eu_intW7 ~ ageW7
wstd_auth01W10 + wstd_eu_intW10 ~ ageW10
wstd_auth01W11 + wstd_eu_intW11 ~ ageW11
wstd_auth01W14 + wstd_eu_intW14 ~ ageW14

ageW7 ~~ ageW7
ageW10 ~~ ageW10
ageW11 ~~ ageW11
ageW14 ~~ ageW14

ageW7 ~~ ageW10 + ageW11 + ageW14
ageW10 ~~ ageW11 + ageW14
ageW11 ~~ ageW14

# incorporate tvc - ethnicity
wstd_auth01W7 + wstd_eu_intW7 ~ ethnic7
wstd_auth01W10 + wstd_eu_intW10 ~ ethnic10
wstd_auth01W11 + wstd_eu_intW11 ~ ethnic11
wstd_auth01W14 + wstd_eu_intW14 ~ ethnic14

ethnic7 ~~ ethnic7
ethnic10 ~~ ethnic10
ethnic11 ~~ ethnic11
ethnic14 ~~ ethnic14

ethnic7 ~~ ethnic10 + ethnic11 + ethnic14
ethnic10 ~~ ethnic11 + ethnic14
ethnic11 ~~ ethnic14

# incorporate tvc – social grade
wstd_auth01W7 + wstd_eu_intW7 ~ p_socgradeW7
wstd_auth01W10 + wstd_eu_intW10 ~ p_socgradeW10
wstd_auth01W11 + wstd_eu_intW11 ~ p_socgradeW11
wstd_auth01W14 + wstd_eu_intW14 ~ p_socgradeW14

p_socgradeW7 ~~ p_socgradeW7
p_socgradeW10 ~~ p_socgradeW10
p_socgradeW11 ~~ p_socgradeW11
p_socgradeW14 ~~ p_socgradeW14

p_socgradeW7 ~~ p_socgradeW10 + p_socgradeW11 + p_socgradeW14
p_socgradeW10 ~~ p_socgradeW11 + p_socgradeW14
p_socgradeW11 ~~ p_socgradeW14

# incorporate tvc – education
wstd_auth01W7 + wstd_eu_intW7 ~ higheduc7
wstd_auth01W10 + wstd_eu_intW10 ~ higheduc10
wstd_auth01W11 + wstd_eu_intW11 ~ higheduc11
wstd_auth01W14 + wstd_eu_intW14 ~ higheduc14

higheduc7 ~~ higheduc7
higheduc10 ~~ higheduc10
higheduc11 ~~ higheduc11
higheduc14 ~~ higheduc14

higheduc7 ~~ higheduc10 + higheduc11 + higheduc14
higheduc10 ~~ higheduc11 + higheduc14
higheduc11 ~~ higheduc14

# incorporate tvc – income
wstd_auth01W7 + wstd_eu_intW7 ~ incomerec7
wstd_auth01W10 + wstd_eu_intW10 ~ incomerec10
wstd_auth01W11 + wstd_eu_intW11 ~ incomerec11
wstd_auth01W14 + wstd_eu_intW14 ~ incomerec14

incomerec7 ~~ incomerec7
incomerec10 ~~ incomerec10
incomerec11 ~~ incomerec11
incomerec14 ~~ incomerec14

incomerec7 ~~ incomerec10 + incomerec11 + incomerec14
incomerec10 ~~ incomerec11 + incomerec14
incomerec11 ~~ incomerec14

'

std_eu_CLPM_L_noEQ_tvc.fit <- lavaan(std_eu_CLPM_L_noEQ_tvc, 
                                 estimator = "MLR",
                                 data = att_dat_low,
                                 missing = 'ML', 
                                 meanstructure = T, 
                                 int.ov.free = T) 
summary(std_eu_CLPM_L_noEQ_tvc.fit, standardized = T)


# Results 

standardizedsolution(std_eu_CLPM_H_tvc.fit)
standardizedsolution(std_eu_CLPM_H_noEQ_tvc.fit)
standardizedsolution(std_eu_CLPM_L_tvc.fit)
standardizedsolution(std_eu_CLPM_L_noEQ_tvc.fit)

#### Multiple-group RI-CLPMs - Ethnicity ####

ethnic_dat <- read.dta13("C:/Replication files_Authoritarianism, Political Attitudes and Vote Choice/RICLPM data_ethnicity.dta")
ethnic_dat_white <- read.dta13("C:/Replication files_Authoritarianism, Political Attitudes and Vote Choice/RICLPM data_ethnicity_white.dta")
ethnic_dat_nonwhite <- read.dta13("C:/Replication files_Authoritarianism, Political Attitudes and Vote Choice/RICLPM data_ethnicity_nonwhite.dta")
## Redistribution 

std_redist_ethU <- '
  # Create between components (random intercepts)
  RIstd_auth01W  =~ 1*std_auth01W7 + 1*std_auth01W10 + 1*std_auth01W11 + 1*std_auth01W14 
  RIstd_redist_prefW =~ 1*std_redistSelf01W7 + 1*std_redistSelf01W10 + 1*std_redistSelf01W11 + 1*std_redistSelf01W14 
  
  # Create within-person centered variables
  wstd_auth01W7 =~ 1*std_auth01W7
  wstd_auth01W10 =~ 1*std_auth01W10
  wstd_auth01W11 =~ 1*std_auth01W11 
  wstd_auth01W14 =~ 1*std_auth01W14

  wstd_redist_prefW7 =~ 1*std_redistSelf01W7
  wstd_redist_prefW10 =~ 1*std_redistSelf01W10
  wstd_redist_prefW11 =~ 1*std_redistSelf01W11
  wstd_redist_prefW14 =~ 1*std_redistSelf01W14

  # Estimate the lagged effects between the within-person centered variables.
  wstd_auth01W10 + wstd_redist_prefW10 ~ wstd_auth01W7 + wstd_redist_prefW7
  wstd_auth01W11 + wstd_redist_prefW11 ~ wstd_auth01W10 + wstd_redist_prefW10
  wstd_auth01W14 + wstd_redist_prefW14 ~ wstd_auth01W11 + wstd_redist_prefW11

  # Estimate the covariance between the within-person centered variables at the first wave. 
  wstd_auth01W7 ~~ wstd_redist_prefW7 # Covariance
  
  # Estimate the covariances between the residuals of the within-person centered variables (the innovations).
  wstd_auth01W10 ~~ wstd_redist_prefW10
  wstd_auth01W11 ~~ wstd_redist_prefW11
  wstd_auth01W14 ~~ wstd_redist_prefW14
  
  
  # Estimate the variance and covariance of the random intercepts. 
  RIstd_auth01W ~~ RIstd_auth01W
  RIstd_redist_prefW ~~ RIstd_redist_prefW
  RIstd_auth01W ~~ RIstd_redist_prefW

  # Estimate the (residual) variance of the within-person centered variables.
  wstd_auth01W7 ~~ wstd_auth01W7 # Variances
  wstd_redist_prefW7 ~~ wstd_redist_prefW7 
  wstd_auth01W10 ~~ wstd_auth01W10 # Residual variances
  wstd_redist_prefW10 ~~ wstd_redist_prefW10 
  wstd_auth01W11 ~~ wstd_auth01W11
  wstd_redist_prefW11 ~~ wstd_redist_prefW11 
  wstd_auth01W14 ~~ wstd_auth01W14 
  wstd_redist_prefW14 ~~ wstd_redist_prefW14 
  
  # Fix error variance of indicators to (1 - alpha)*var
std_auth01W10 ~~ 0.0058*std_auth01W10
std_auth01W11 ~~ 0.002*std_auth01W11
std_auth01W14 ~~ 0.00725*std_auth01W14
std_redistSelf01W10 ~~ 0.01038*std_redistSelf01W10
std_redistSelf01W11 ~~ 0.00933*std_redistSelf01W11
std_redistSelf01W14 ~~ 0*std_redistSelf01W14

'

std_redist_ethU.fit <- lavaan(std_redist_ethU, 
                              data = ethnic_dat,
                              group = "ethnic_group",
                              estimator = "MLR",
                              missing = 'ML', 
                              meanstructure = T, 
                              int.ov.free = T) 
summary(std_redist_ethU.fit, standardized = T)

std_redist_ethC <- '
  # Create between components (random intercepts)
  RIstd_auth01W  =~ 1*std_auth01W7 + 1*std_auth01W10 + 1*std_auth01W11 + 1*std_auth01W14 
  RIstd_redist_prefW =~ 1*std_redistSelf01W7 + 1*std_redistSelf01W10 + 1*std_redistSelf01W11 + 1*std_redistSelf01W14 
  
  # Create within-person centered variables
  wstd_auth01W7 =~ 1*std_auth01W7
  wstd_auth01W10 =~ 1*std_auth01W10
  wstd_auth01W11 =~ 1*std_auth01W11 
  wstd_auth01W14 =~ 1*std_auth01W14

  wstd_redist_prefW7 =~ 1*std_redistSelf01W7
  wstd_redist_prefW10 =~ 1*std_redistSelf01W10
  wstd_redist_prefW11 =~ 1*std_redistSelf01W11
  wstd_redist_prefW14 =~ 1*std_redistSelf01W14

  # Estimate the lagged effects between the within-person centered variables.
  wstd_auth01W10 ~ c(a1, a1)*wstd_auth01W7 + c(b1, b1)*wstd_redist_prefW7 
  wstd_redist_prefW10 ~ c(c1, c1)*wstd_auth01W7 + c(d1, d1)*wstd_redist_prefW7
  wstd_auth01W11 ~ c(a2, a2)*wstd_auth01W10 + c(b2, b2)*wstd_redist_prefW10
  wstd_redist_prefW11 ~ c(c2, c2)*wstd_auth01W10 + c(d2, d2)*wstd_redist_prefW10
  wstd_auth01W14 ~ c(a3, a3)*wstd_auth01W11 + c(b3, b3)*wstd_redist_prefW11
  wstd_redist_prefW14 ~ c(c3, c3)*wstd_auth01W11 + c(d3, d3)*wstd_redist_prefW11

  # Estimate the covariance between the within-person centered variables at the first wave. 
  wstd_auth01W7 ~~ wstd_redist_prefW7 # Covariance
  
  # Estimate the covariances between the residuals of the within-person centered variables (the innovations).
  wstd_auth01W10 ~~ wstd_redist_prefW10
  wstd_auth01W11 ~~ wstd_redist_prefW11
  wstd_auth01W14 ~~ wstd_redist_prefW14
  
  
  # Estimate the variance and covariance of the random intercepts. 
  RIstd_auth01W ~~ RIstd_auth01W
  RIstd_redist_prefW ~~ RIstd_redist_prefW
  RIstd_auth01W ~~ RIstd_redist_prefW

  # Estimate the (residual) variance of the within-person centered variables.
  wstd_auth01W7 ~~ wstd_auth01W7 # Variances
  wstd_redist_prefW7 ~~ wstd_redist_prefW7 
  wstd_auth01W10 ~~ wstd_auth01W10 # Residual variances
  wstd_redist_prefW10 ~~ wstd_redist_prefW10 
  wstd_auth01W11 ~~ wstd_auth01W11
  wstd_redist_prefW11 ~~ wstd_redist_prefW11 
  wstd_auth01W14 ~~ wstd_auth01W14 
  wstd_redist_prefW14 ~~ wstd_redist_prefW14 
  
  # Fix error variance of indicators to (1 - alpha)*var
std_auth01W10 ~~ 0.0058*std_auth01W10
std_auth01W11 ~~ 0.002*std_auth01W11
std_auth01W14 ~~ 0.00725*std_auth01W14
std_redistSelf01W10 ~~ 0.01038*std_redistSelf01W10
std_redistSelf01W11 ~~ 0.00933*std_redistSelf01W11
std_redistSelf01W14 ~~ 0*std_redistSelf01W14

'

std_redist_ethC.fit <- lavaan(std_redist_ethC, 
                              data = ethnic_dat,
                              group = "ethnic_group",
                              estimator = "MLR",
                              missing = 'ML', 
                              meanstructure = T, 
                              int.ov.free = T) 

anova(std_redist_ethU.fit, std_redist_ethC.fit)

## Immigration

std_immig_ethU <- '
 # Create between components (random intercepts)
  RIstd_auth01W  =~ 1*std_auth01W7 + 1*std_auth01W10 + 1*std_auth01W11 + 1*std_auth01W14 
  RIim_prefW =~ 1*std_im_pref_2W7 + 1*std_im_pref_2W10 + 1*std_im_pref_2W11 + 1*std_im_pref_2W14 
  
  # Create within-person centered variables
  wstd_auth01W7 =~ 1*std_auth01W7
  wstd_auth01W10 =~ 1*std_auth01W10
  wstd_auth01W11 =~ 1*std_auth01W11 
  wstd_auth01W14 =~ 1*std_auth01W14

  wim_prefW7 =~ 1*std_im_pref_2W7
  wim_prefW10 =~ 1*std_im_pref_2W10
  wim_prefW11 =~ 1*std_im_pref_2W11
  wim_prefW14 =~ 1*std_im_pref_2W14

  # Estimate the lagged effects between the within-person centered variables.
  wstd_auth01W10 + wim_prefW10 ~ wstd_auth01W7 + wim_prefW7
  wstd_auth01W11 + wim_prefW11 ~ wstd_auth01W10 + wim_prefW10
  wstd_auth01W14 + wim_prefW14 ~ wstd_auth01W11 + wim_prefW11

  # Estimate the covariance between the within-person centered variables at the first wave. 
  wstd_auth01W7 ~~ wim_prefW7 # Covariance
  
  # Estimate the covariances between the residuals of the within-person centered variables (the innovations).
  wstd_auth01W10 ~~ wim_prefW10
  wstd_auth01W11 ~~ wim_prefW11
  wstd_auth01W14 ~~ wim_prefW14
  
  
  # Estimate the variance and covariance of the random intercepts. 
  RIstd_auth01W ~~ RIstd_auth01W
  RIim_prefW ~~ RIim_prefW
  RIstd_auth01W ~~ RIim_prefW

  # Estimate the (residual) variance of the within-person centered variables.
  wstd_auth01W7 ~~ wstd_auth01W7 # Variances
  wim_prefW7 ~~ wim_prefW7 
  wstd_auth01W10 ~~ wstd_auth01W10 # Residual variances
  wim_prefW10 ~~ wim_prefW10 
  wstd_auth01W11 ~~ wstd_auth01W11
  wim_prefW11 ~~ wim_prefW11 
  wstd_auth01W14 ~~ wstd_auth01W14 
  wim_prefW14 ~~ wim_prefW14 
  
  # Fix error variance of indicators to (1 - alpha)*var
std_auth01W7 ~~ 0.02*std_auth01W7
std_auth01W10 ~~ 0.016*std_auth01W10
std_auth01W11 ~~ 0.018*std_auth01W11
std_auth01W14 ~~ 0.019*std_auth01W14
std_im_pref_2W7 ~~ 0.0147*std_im_pref_2W7
std_im_pref_2W10 ~~ 0.000155*std_im_pref_2W10
std_im_pref_2W11 ~~ 0.000147*std_im_pref_2W11
std_im_pref_2W14 ~~ 0.000512*std_im_pref_2W14

  '
std_immig_ethU.fit <- lavaan(std_immig_ethU, 
                             data = ethnic_dat, 
                             group = "ethnic_group",
                             estimator = "MLR",
                             missing = 'ML',
                             meanstructure = T, 
                             int.ov.free = T) 
summary(std_immig_ethU.fit, standardized = T)

std_immig_ethC <- '
 # Create between components (random intercepts)
  RIstd_auth01W  =~ 1*std_auth01W7 + 1*std_auth01W10 + 1*std_auth01W11 + 1*std_auth01W14 
  RIim_prefW =~ 1*std_im_pref_2W7 + 1*std_im_pref_2W10 + 1*std_im_pref_2W11 + 1*std_im_pref_2W14 
  
  # Create within-person centered variables
  wstd_auth01W7 =~ 1*std_auth01W7
  wstd_auth01W10 =~ 1*std_auth01W10
  wstd_auth01W11 =~ 1*std_auth01W11 
  wstd_auth01W14 =~ 1*std_auth01W14

  wim_prefW7 =~ 1*std_im_pref_2W7
  wim_prefW10 =~ 1*std_im_pref_2W10
  wim_prefW11 =~ 1*std_im_pref_2W11
  wim_prefW14 =~ 1*std_im_pref_2W14

  # Estimate the lagged effects between the within-person centered variables.
  wstd_auth01W10 ~ c(a1, a1)*wstd_auth01W7 + c(b1, b1)*wim_prefW7
  wim_prefW10 ~ c(c1, c1)*wstd_auth01W7 + c(d1, d1)*wim_prefW7
  wstd_auth01W11 ~ c(a2, a2)*wstd_auth01W10 + c(b2, b2)*wim_prefW10
  wim_prefW11 ~ c(c2, c2)*wstd_auth01W10 + c(d2, d2)*wim_prefW10
  wstd_auth01W14 ~ c(a3, a3)*wstd_auth01W11 + c(b3, b3)*wim_prefW11
  wim_prefW14 ~ c(c3, c3)*wstd_auth01W11 + c(d3, d3)*wim_prefW11

  # Estimate the covariance between the within-person centered variables at the first wave. 
  wstd_auth01W7 ~~ wim_prefW7 # Covariance
  
  # Estimate the covariances between the residuals of the within-person centered variables (the innovations).
  wstd_auth01W10 ~~ wim_prefW10
  wstd_auth01W11 ~~ wim_prefW11
  wstd_auth01W14 ~~ wim_prefW14
  
  
  # Estimate the variance and covariance of the random intercepts. 
  RIstd_auth01W ~~ RIstd_auth01W
  RIim_prefW ~~ RIim_prefW
  RIstd_auth01W ~~ RIim_prefW

  # Estimate the (residual) variance of the within-person centered variables.
  wstd_auth01W7 ~~ wstd_auth01W7 # Variances
  wim_prefW7 ~~ wim_prefW7 
  wstd_auth01W10 ~~ wstd_auth01W10 # Residual variances
  wim_prefW10 ~~ wim_prefW10 
  wstd_auth01W11 ~~ wstd_auth01W11
  wim_prefW11 ~~ wim_prefW11 
  wstd_auth01W14 ~~ wstd_auth01W14 
  wim_prefW14 ~~ wim_prefW14 
  
  # Fix error variance of indicators to (1 - alpha)*var
std_auth01W7 ~~ 0.02*std_auth01W7
std_auth01W10 ~~ 0.016*std_auth01W10
std_auth01W11 ~~ 0.018*std_auth01W11
std_auth01W14 ~~ 0.019*std_auth01W14
std_im_pref_2W7 ~~ 0.0147*std_im_pref_2W7
std_im_pref_2W10 ~~ 0.000155*std_im_pref_2W10
std_im_pref_2W11 ~~ 0.000147*std_im_pref_2W11
std_im_pref_2W14 ~~ 0.000512*std_im_pref_2W14

  '
std_immig_ethC.fit <- lavaan(std_immig_ethC, 
                             data = ethnic_dat, 
                             group = "ethnic_group",
                             estimator = "MLR",
                             missing = 'ML',
                             meanstructure = T, 
                             int.ov.free = T) 
summary(std_immig_ethC.fit, standardized = T)

anova(std_immig_ethU.fit, std_immig_ethC.fit)

## EU integration

std_eu_ethU <- '
  # Create between components (random intercepts)
  RIstd_auth01W  =~ 1*std_auth01W7 + 1*std_auth01W10 + 1*std_auth01W11 + 1*std_auth01W14 
  RIstd_eu_intW =~ 1*std_eu_intW7 + 1*std_eu_intW10 + 1*std_eu_intW11 + 1*std_eu_intW14 
  
  # Create within-person centered variables
  wstd_auth01W7 =~ 1*std_auth01W7
  wstd_auth01W10 =~ 1*std_auth01W10
  wstd_auth01W11 =~ 1*std_auth01W11 
  wstd_auth01W14 =~ 1*std_auth01W14

  wstd_eu_intW7 =~ 1*std_eu_intW7
  wstd_eu_intW10 =~ 1*std_eu_intW10
  wstd_eu_intW11 =~ 1*std_eu_intW11
  wstd_eu_intW14 =~ 1*std_eu_intW14

  # Estimate the lagged effects between the within-person centered variables.
  wstd_auth01W10 + wstd_eu_intW10 ~ wstd_auth01W7 + wstd_eu_intW7
  wstd_auth01W11 + wstd_eu_intW11 ~ wstd_auth01W10 + wstd_eu_intW10
  wstd_auth01W14 + wstd_eu_intW14 ~ wstd_auth01W11 + wstd_eu_intW11

  # Estimate the covariance between the within-person centered variables at the first wave. 
  wstd_auth01W7 ~~ wstd_eu_intW7 # Covariance
  
  # Estimate the covariances between the residuals of the within-person centered variables (the innovations).
  wstd_auth01W10 ~~ wstd_eu_intW10
  wstd_auth01W11 ~~ wstd_eu_intW11
  wstd_auth01W14 ~~ wstd_eu_intW14
  
  
  # Estimate the variance and covariance of the random intercepts. 
  RIstd_auth01W ~~ RIstd_auth01W
  RIstd_eu_intW ~~ RIstd_eu_intW
  RIstd_auth01W ~~ RIstd_eu_intW

  # Estimate the (residual) variance of the within-person centered variables.
  wstd_auth01W7 ~~ wstd_auth01W7 # Variances
  wstd_eu_intW7 ~~ wstd_eu_intW7 
  wstd_auth01W10 ~~ wstd_auth01W10 # Residual variances
  wstd_eu_intW10 ~~ wstd_eu_intW10 
  wstd_auth01W11 ~~ wstd_auth01W11
  wstd_eu_intW11 ~~ wstd_eu_intW11 
  wstd_auth01W14 ~~ wstd_auth01W14 
  wstd_eu_intW14 ~~ wstd_eu_intW14 
  
  # Fix error variance of indicators to (1 - alpha)*var
std_auth01W10 ~~ 0.0058*std_auth01W10
std_auth01W11 ~~ 0.002*std_auth01W11
std_auth01W14 ~~ 0.00725*std_auth01W14
std_eu_intW10 ~~ 0.0028*std_eu_intW10
std_eu_intW11 ~~ 0.00194*std_eu_intW11
std_eu_intW14 ~~ 0*std_eu_intW14


'

std_eu_ethU.fit <- lavaan(std_eu_ethU, 
                          estimator = "MLR",
                          data = ethnic_dat,
                          group = "ethnic_group",
                          missing = 'ML', 
                          meanstructure = T, 
                          int.ov.free = T) 
summary(std_eu_ethU.fit, standardized = T)

std_eu_ethC <- '
  # Create between components (random intercepts)
  RIstd_auth01W  =~ 1*std_auth01W7 + 1*std_auth01W10 + 1*std_auth01W11 + 1*std_auth01W14 
  RIstd_eu_intW =~ 1*std_eu_intW7 + 1*std_eu_intW10 + 1*std_eu_intW11 + 1*std_eu_intW14 
  
  # Create within-person centered variables
  wstd_auth01W7 =~ 1*std_auth01W7
  wstd_auth01W10 =~ 1*std_auth01W10
  wstd_auth01W11 =~ 1*std_auth01W11 
  wstd_auth01W14 =~ 1*std_auth01W14

  wstd_eu_intW7 =~ 1*std_eu_intW7
  wstd_eu_intW10 =~ 1*std_eu_intW10
  wstd_eu_intW11 =~ 1*std_eu_intW11
  wstd_eu_intW14 =~ 1*std_eu_intW14
  
  # Estimate the lagged effects between the within-person centered variables.
  wstd_auth01W10 ~ c(a1, a1)*wstd_auth01W7 + c(b1, b1)*wstd_eu_intW7
  wstd_eu_intW10 ~ c(c1, c1)*wstd_auth01W7 + c(d1, d1)*wstd_eu_intW7
  wstd_auth01W11 ~ c(a2, a2)*wstd_auth01W10 + c(b2, b2)*wstd_eu_intW10
  wstd_eu_intW11 ~ c(c2, c2)*wstd_auth01W10 + c(d2, d2)*wstd_eu_intW10
  wstd_auth01W14 ~ c(a3, a3)*wstd_auth01W11 + c(b3, b3)*wstd_eu_intW11
  wstd_eu_intW14 ~ c(c3, c3)*wstd_auth01W11 + c(d3, d3)*wstd_eu_intW11

  # Estimate the covariance between the within-person centered variables at the first wave. 
  wstd_auth01W7 ~~ wstd_eu_intW7 # Covariance
  
  # Estimate the covariances between the residuals of the within-person centered variables (the innovations).
  wstd_auth01W10 ~~ wstd_eu_intW10
  wstd_auth01W11 ~~ wstd_eu_intW11
  wstd_auth01W14 ~~ wstd_eu_intW14
  
  
  # Estimate the variance and covariance of the random intercepts. 
  RIstd_auth01W ~~ RIstd_auth01W
  RIstd_eu_intW ~~ RIstd_eu_intW
  RIstd_auth01W ~~ RIstd_eu_intW

  # Estimate the (residual) variance of the within-person centered variables.
  wstd_auth01W7 ~~ wstd_auth01W7 # Variances
  wstd_eu_intW7 ~~ wstd_eu_intW7 
  wstd_auth01W10 ~~ wstd_auth01W10 # Residual variances
  wstd_eu_intW10 ~~ wstd_eu_intW10 
  wstd_auth01W11 ~~ wstd_auth01W11
  wstd_eu_intW11 ~~ wstd_eu_intW11 
  wstd_auth01W14 ~~ wstd_auth01W14 
  wstd_eu_intW14 ~~ wstd_eu_intW14 
  
  # Fix error variance of indicators to (1 - alpha)*var
std_auth01W10 ~~ 0.0058*std_auth01W10
std_auth01W11 ~~ 0.002*std_auth01W11
std_auth01W14 ~~ 0.00725*std_auth01W14
std_eu_intW10 ~~ 0.0028*std_eu_intW10
std_eu_intW11 ~~ 0.00194*std_eu_intW11
std_eu_intW14 ~~ 0*std_eu_intW14
'

std_eu_ethC.fit <- lavaan(std_eu_ethC, 
                          estimator = "MLR",
                          data = ethnic_dat,
                          group = "ethnic_group",
                          missing = 'ML', 
                          meanstructure = T, 
                          int.ov.free = T) 
summary(std_eu_ethC.fit, standardized = T)

anova(std_eu_ethC.fit, std_eu_ethU.fit)

#### Multiple-group CLPMs - Ethnicity ####

## Redistribution 

std_redist_ethU_CLPM <- '
  # Create between components (random intercepts)
  RIstd_auth01W  =~ 1*std_auth01W7 + 1*std_auth01W10 + 1*std_auth01W11 + 1*std_auth01W14 
  RIstd_redist_prefW =~ 1*std_redistSelf01W7 + 1*std_redistSelf01W10 + 1*std_redistSelf01W11 + 1*std_redistSelf01W14 
  
  # Create within-person centered variables
  wstd_auth01W7 =~ 1*std_auth01W7
  wstd_auth01W10 =~ 1*std_auth01W10
  wstd_auth01W11 =~ 1*std_auth01W11 
  wstd_auth01W14 =~ 1*std_auth01W14

  wstd_redist_prefW7 =~ 1*std_redistSelf01W7
  wstd_redist_prefW10 =~ 1*std_redistSelf01W10
  wstd_redist_prefW11 =~ 1*std_redistSelf01W11
  wstd_redist_prefW14 =~ 1*std_redistSelf01W14

  # Estimate the lagged effects between the within-person centered variables.
  wstd_auth01W10 + wstd_redist_prefW10 ~ wstd_auth01W7 + wstd_redist_prefW7
  wstd_auth01W11 + wstd_redist_prefW11 ~ wstd_auth01W10 + wstd_redist_prefW10
  wstd_auth01W14 + wstd_redist_prefW14 ~ wstd_auth01W11 + wstd_redist_prefW11

  # Estimate the covariance between the within-person centered variables at the first wave. 
  wstd_auth01W7 ~~ wstd_redist_prefW7 # Covariance
  
  # Estimate the covariances between the residuals of the within-person centered variables (the innovations).
  wstd_auth01W10 ~~ wstd_redist_prefW10
  wstd_auth01W11 ~~ wstd_redist_prefW11
  wstd_auth01W14 ~~ wstd_redist_prefW14
  
  
  # Fix the variance and covariance of the random intercepts to 0
  RIstd_auth01W ~~ 0*RIstd_auth01W
  RIstd_redist_prefW ~~ 0*RIstd_redist_prefW
  RIstd_auth01W ~~ 0*RIstd_redist_prefW

  # Estimate the (residual) variance of the within-person centered variables.
  wstd_auth01W7 ~~ wstd_auth01W7 # Variances
  wstd_redist_prefW7 ~~ wstd_redist_prefW7 
  wstd_auth01W10 ~~ wstd_auth01W10 # Residual variances
  wstd_redist_prefW10 ~~ wstd_redist_prefW10 
  wstd_auth01W11 ~~ wstd_auth01W11
  wstd_redist_prefW11 ~~ wstd_redist_prefW11 
  wstd_auth01W14 ~~ wstd_auth01W14 
  wstd_redist_prefW14 ~~ wstd_redist_prefW14 
  
  # Fix error variance of indicators to (1 - alpha)*var
std_auth01W10 ~~ 0.0058*std_auth01W10
std_auth01W11 ~~ 0.002*std_auth01W11
std_auth01W14 ~~ 0.00725*std_auth01W14
std_redistSelf01W10 ~~ 0.01038*std_redistSelf01W10
std_redistSelf01W11 ~~ 0.00933*std_redistSelf01W11
std_redistSelf01W14 ~~ 0*std_redistSelf01W14

'

std_redist_ethU_CLPM.fit <- lavaan(std_redist_ethU_CLPM, 
                              data = ethnic_dat,
                              group = "ethnic_group",
                              estimator = "MLR",
                              missing = 'ML', 
                              meanstructure = T, 
                              int.ov.free = T) 
summary(std_redist_ethU_CLPM.fit, standardized = T)

std_redist_ethC_CLPM <- '
  # Create between components (random intercepts)
  RIstd_auth01W  =~ 1*std_auth01W7 + 1*std_auth01W10 + 1*std_auth01W11 + 1*std_auth01W14 
  RIstd_redist_prefW =~ 1*std_redistSelf01W7 + 1*std_redistSelf01W10 + 1*std_redistSelf01W11 + 1*std_redistSelf01W14 
  
  # Create within-person centered variables
  wstd_auth01W7 =~ 1*std_auth01W7
  wstd_auth01W10 =~ 1*std_auth01W10
  wstd_auth01W11 =~ 1*std_auth01W11 
  wstd_auth01W14 =~ 1*std_auth01W14

  wstd_redist_prefW7 =~ 1*std_redistSelf01W7
  wstd_redist_prefW10 =~ 1*std_redistSelf01W10
  wstd_redist_prefW11 =~ 1*std_redistSelf01W11
  wstd_redist_prefW14 =~ 1*std_redistSelf01W14

  # Estimate the lagged effects between the within-person centered variables.
  wstd_auth01W10 ~ c(a1, a1)*wstd_auth01W7 + c(b1, b1)*wstd_redist_prefW7 
  wstd_redist_prefW10 ~ c(c1, c1)*wstd_auth01W7 + c(d1, d1)*wstd_redist_prefW7
  wstd_auth01W11 ~ c(a2, a2)*wstd_auth01W10 + c(b2, b2)*wstd_redist_prefW10
  wstd_redist_prefW11 ~ c(c2, c2)*wstd_auth01W10 + c(d2, d2)*wstd_redist_prefW10
  wstd_auth01W14 ~ c(a3, a3)*wstd_auth01W11 + c(b3, b3)*wstd_redist_prefW11
  wstd_redist_prefW14 ~ c(c3, c3)*wstd_auth01W11 + c(d3, d3)*wstd_redist_prefW11

  # Estimate the covariance between the within-person centered variables at the first wave. 
  wstd_auth01W7 ~~ wstd_redist_prefW7 # Covariance
  
  # Estimate the covariances between the residuals of the within-person centered variables (the innovations).
  wstd_auth01W10 ~~ wstd_redist_prefW10
  wstd_auth01W11 ~~ wstd_redist_prefW11
  wstd_auth01W14 ~~ wstd_redist_prefW14
  
  # Fix the variance and covariance of the random intercepts to 0
  RIstd_auth01W ~~ 0*RIstd_auth01W
  RIstd_redist_prefW ~~ 0*RIstd_redist_prefW
  RIstd_auth01W ~~ 0*RIstd_redist_prefW

  # Estimate the (residual) variance of the within-person centered variables.
  wstd_auth01W7 ~~ wstd_auth01W7 # Variances
  wstd_redist_prefW7 ~~ wstd_redist_prefW7 
  wstd_auth01W10 ~~ wstd_auth01W10 # Residual variances
  wstd_redist_prefW10 ~~ wstd_redist_prefW10 
  wstd_auth01W11 ~~ wstd_auth01W11
  wstd_redist_prefW11 ~~ wstd_redist_prefW11 
  wstd_auth01W14 ~~ wstd_auth01W14 
  wstd_redist_prefW14 ~~ wstd_redist_prefW14 
  
  # Fix error variance of indicators to (1 - alpha)*var
std_auth01W10 ~~ 0.0058*std_auth01W10
std_auth01W11 ~~ 0.002*std_auth01W11
std_auth01W14 ~~ 0.00725*std_auth01W14
std_redistSelf01W10 ~~ 0.01038*std_redistSelf01W10
std_redistSelf01W11 ~~ 0.00933*std_redistSelf01W11
std_redistSelf01W14 ~~ 0*std_redistSelf01W14

'

std_redist_ethC_CLPM.fit <- lavaan(std_redist_ethC_CLPM, 
                              data = ethnic_dat,
                              group = "ethnic_group",
                              estimator = "MLR",
                              missing = 'ML', 
                              meanstructure = T, 
                              int.ov.free = T) 

anova(std_redist_ethU_CLPM.fit, std_redist_ethC_CLPM.fit)

## Immigration

std_immig_ethU_CLPM <- '
 # Create between components (random intercepts)
  RIstd_auth01W  =~ 1*std_auth01W7 + 1*std_auth01W10 + 1*std_auth01W11 + 1*std_auth01W14 
  RIim_prefW =~ 1*std_im_pref_2W7 + 1*std_im_pref_2W10 + 1*std_im_pref_2W11 + 1*std_im_pref_2W14 
  
  # Create within-person centered variables
  wstd_auth01W7 =~ 1*std_auth01W7
  wstd_auth01W10 =~ 1*std_auth01W10
  wstd_auth01W11 =~ 1*std_auth01W11 
  wstd_auth01W14 =~ 1*std_auth01W14

  wim_prefW7 =~ 1*std_im_pref_2W7
  wim_prefW10 =~ 1*std_im_pref_2W10
  wim_prefW11 =~ 1*std_im_pref_2W11
  wim_prefW14 =~ 1*std_im_pref_2W14

  # Estimate the lagged effects between the within-person centered variables.
  wstd_auth01W10 + wim_prefW10 ~ wstd_auth01W7 + wim_prefW7
  wstd_auth01W11 + wim_prefW11 ~ wstd_auth01W10 + wim_prefW10
  wstd_auth01W14 + wim_prefW14 ~ wstd_auth01W11 + wim_prefW11

  # Estimate the covariance between the within-person centered variables at the first wave. 
  wstd_auth01W7 ~~ wim_prefW7 # Covariance
  
  # Estimate the covariances between the residuals of the within-person centered variables (the innovations).
  wstd_auth01W10 ~~ wim_prefW10
  wstd_auth01W11 ~~ wim_prefW11
  wstd_auth01W14 ~~ wim_prefW14
  
  
  # Fix the variance and covariance of the random intercepts to 0
  RIstd_auth01W ~~ 0*RIstd_auth01W
  RIim_prefW ~~ 0*RIim_prefW
  RIstd_auth01W ~~ 0*RIim_prefW

  # Estimate the (residual) variance of the within-person centered variables.
  wstd_auth01W7 ~~ wstd_auth01W7 # Variances
  wim_prefW7 ~~ wim_prefW7 
  wstd_auth01W10 ~~ wstd_auth01W10 # Residual variances
  wim_prefW10 ~~ wim_prefW10 
  wstd_auth01W11 ~~ wstd_auth01W11
  wim_prefW11 ~~ wim_prefW11 
  wstd_auth01W14 ~~ wstd_auth01W14 
  wim_prefW14 ~~ wim_prefW14 
  
  # Fix error variance of indicators to (1 - alpha)*var
std_auth01W7 ~~ 0.02*std_auth01W7
std_auth01W10 ~~ 0.016*std_auth01W10
std_auth01W11 ~~ 0.018*std_auth01W11
std_auth01W14 ~~ 0.019*std_auth01W14
std_im_pref_2W7 ~~ 0.0147*std_im_pref_2W7
std_im_pref_2W10 ~~ 0.000155*std_im_pref_2W10
std_im_pref_2W11 ~~ 0.000147*std_im_pref_2W11
std_im_pref_2W14 ~~ 0.000512*std_im_pref_2W14

  '
std_immig_ethU_CLPM.fit <- lavaan(std_immig_ethU_CLPM, 
                             data = ethnic_dat, 
                             group = "ethnic_group",
                             estimator = "MLR",
                             missing = 'ML',
                             meanstructure = T, 
                             int.ov.free = T) 
summary(std_immig_ethU_CLPM.fit, standardized = T)

std_immig_ethC_CLPM <- '
 # Create between components (random intercepts)
  RIstd_auth01W  =~ 1*std_auth01W7 + 1*std_auth01W10 + 1*std_auth01W11 + 1*std_auth01W14 
  RIim_prefW =~ 1*std_im_pref_2W7 + 1*std_im_pref_2W10 + 1*std_im_pref_2W11 + 1*std_im_pref_2W14 
  
  # Create within-person centered variables
  wstd_auth01W7 =~ 1*std_auth01W7
  wstd_auth01W10 =~ 1*std_auth01W10
  wstd_auth01W11 =~ 1*std_auth01W11 
  wstd_auth01W14 =~ 1*std_auth01W14

  wim_prefW7 =~ 1*std_im_pref_2W7
  wim_prefW10 =~ 1*std_im_pref_2W10
  wim_prefW11 =~ 1*std_im_pref_2W11
  wim_prefW14 =~ 1*std_im_pref_2W14

  # Estimate the lagged effects between the within-person centered variables.
  wstd_auth01W10 ~ c(a1, a1)*wstd_auth01W7 + c(b1, b1)*wim_prefW7
  wim_prefW10 ~ c(c1, c1)*wstd_auth01W7 + c(d1, d1)*wim_prefW7
  wstd_auth01W11 ~ c(a2, a2)*wstd_auth01W10 + c(b2, b2)*wim_prefW10
  wim_prefW11 ~ c(c2, c2)*wstd_auth01W10 + c(d2, d2)*wim_prefW10
  wstd_auth01W14 ~ c(a3, a3)*wstd_auth01W11 + c(b3, b3)*wim_prefW11
  wim_prefW14 ~ c(c3, c3)*wstd_auth01W11 + c(d3, d3)*wim_prefW11

  # Estimate the covariance between the within-person centered variables at the first wave. 
  wstd_auth01W7 ~~ wim_prefW7 # Covariance
  
  # Estimate the covariances between the residuals of the within-person centered variables (the innovations).
  wstd_auth01W10 ~~ wim_prefW10
  wstd_auth01W11 ~~ wim_prefW11
  wstd_auth01W14 ~~ wim_prefW14
  
  
  # Fix the variance and covariance of the random intercepts to 0
  RIstd_auth01W ~~ 0*RIstd_auth01W
  RIim_prefW ~~ 0*RIim_prefW
  RIstd_auth01W ~~ 0*RIim_prefW

  # Estimate the (residual) variance of the within-person centered variables.
  wstd_auth01W7 ~~ wstd_auth01W7 # Variances
  wim_prefW7 ~~ wim_prefW7 
  wstd_auth01W10 ~~ wstd_auth01W10 # Residual variances
  wim_prefW10 ~~ wim_prefW10 
  wstd_auth01W11 ~~ wstd_auth01W11
  wim_prefW11 ~~ wim_prefW11 
  wstd_auth01W14 ~~ wstd_auth01W14 
  wim_prefW14 ~~ wim_prefW14 
  
  # Fix error variance of indicators to (1 - alpha)*var
std_auth01W7 ~~ 0.02*std_auth01W7
std_auth01W10 ~~ 0.016*std_auth01W10
std_auth01W11 ~~ 0.018*std_auth01W11
std_auth01W14 ~~ 0.019*std_auth01W14
std_im_pref_2W7 ~~ 0.0147*std_im_pref_2W7
std_im_pref_2W10 ~~ 0.000155*std_im_pref_2W10
std_im_pref_2W11 ~~ 0.000147*std_im_pref_2W11
std_im_pref_2W14 ~~ 0.000512*std_im_pref_2W14

  '
std_immig_ethC_CLPM.fit <- lavaan(std_immig_ethC_CLPM, 
                             data = ethnic_dat, 
                             group = "ethnic_group",
                             estimator = "MLR",
                             missing = 'ML',
                             meanstructure = T, 
                             int.ov.free = T) 
summary(std_immig_ethC_CLPM.fit, standardized = T)

anova(std_immig_ethU_CLPM.fit, std_immig_ethC_CLPM.fit)

## EU integration

std_eu_ethU_CLPM <- '
  # Create between components (random intercepts)
  RIstd_auth01W  =~ 1*std_auth01W7 + 1*std_auth01W10 + 1*std_auth01W11 + 1*std_auth01W14 
  RIstd_eu_intW =~ 1*std_eu_intW7 + 1*std_eu_intW10 + 1*std_eu_intW11 + 1*std_eu_intW14 
  
  # Create within-person centered variables
  wstd_auth01W7 =~ 1*std_auth01W7
  wstd_auth01W10 =~ 1*std_auth01W10
  wstd_auth01W11 =~ 1*std_auth01W11 
  wstd_auth01W14 =~ 1*std_auth01W14

  wstd_eu_intW7 =~ 1*std_eu_intW7
  wstd_eu_intW10 =~ 1*std_eu_intW10
  wstd_eu_intW11 =~ 1*std_eu_intW11
  wstd_eu_intW14 =~ 1*std_eu_intW14

  # Estimate the lagged effects between the within-person centered variables.
  wstd_auth01W10 + wstd_eu_intW10 ~ wstd_auth01W7 + wstd_eu_intW7
  wstd_auth01W11 + wstd_eu_intW11 ~ wstd_auth01W10 + wstd_eu_intW10
  wstd_auth01W14 + wstd_eu_intW14 ~ wstd_auth01W11 + wstd_eu_intW11

  # Estimate the covariance between the within-person centered variables at the first wave. 
  wstd_auth01W7 ~~ wstd_eu_intW7 # Covariance
  
  # Estimate the covariances between the residuals of the within-person centered variables (the innovations).
  wstd_auth01W10 ~~ wstd_eu_intW10
  wstd_auth01W11 ~~ wstd_eu_intW11
  wstd_auth01W14 ~~ wstd_eu_intW14
  
  
  # Fix the variance and covariance of the random intercepts to 0
  RIstd_auth01W ~~ 0*RIstd_auth01W
  RIstd_eu_intW ~~ 0*RIstd_eu_intW
  RIstd_auth01W ~~ 0*RIstd_eu_intW

  # Estimate the (residual) variance of the within-person centered variables.
  wstd_auth01W7 ~~ wstd_auth01W7 # Variances
  wstd_eu_intW7 ~~ wstd_eu_intW7 
  wstd_auth01W10 ~~ wstd_auth01W10 # Residual variances
  wstd_eu_intW10 ~~ wstd_eu_intW10 
  wstd_auth01W11 ~~ wstd_auth01W11
  wstd_eu_intW11 ~~ wstd_eu_intW11 
  wstd_auth01W14 ~~ wstd_auth01W14 
  wstd_eu_intW14 ~~ wstd_eu_intW14 
  
  # Fix error variance of indicators to (1 - alpha)*var
std_auth01W10 ~~ 0.0058*std_auth01W10
std_auth01W11 ~~ 0.002*std_auth01W11
std_auth01W14 ~~ 0.00725*std_auth01W14
std_eu_intW10 ~~ 0.0028*std_eu_intW10
std_eu_intW11 ~~ 0.00194*std_eu_intW11
std_eu_intW14 ~~ 0*std_eu_intW14


'

std_eu_ethU_CLPM.fit <- lavaan(std_eu_ethU_CLPM, 
                          estimator = "MLR",
                          data = ethnic_dat,
                          group = "ethnic_group",
                          missing = 'ML', 
                          meanstructure = T, 
                          int.ov.free = T) 
summary(std_eu_ethU_CLPM.fit, standardized = T)

std_eu_ethC_CLPM <- '
  # Create between components (random intercepts)
  RIstd_auth01W  =~ 1*std_auth01W7 + 1*std_auth01W10 + 1*std_auth01W11 + 1*std_auth01W14 
  RIstd_eu_intW =~ 1*std_eu_intW7 + 1*std_eu_intW10 + 1*std_eu_intW11 + 1*std_eu_intW14 
  
  # Create within-person centered variables
  wstd_auth01W7 =~ 1*std_auth01W7
  wstd_auth01W10 =~ 1*std_auth01W10
  wstd_auth01W11 =~ 1*std_auth01W11 
  wstd_auth01W14 =~ 1*std_auth01W14

  wstd_eu_intW7 =~ 1*std_eu_intW7
  wstd_eu_intW10 =~ 1*std_eu_intW10
  wstd_eu_intW11 =~ 1*std_eu_intW11
  wstd_eu_intW14 =~ 1*std_eu_intW14
  
  # Estimate the lagged effects between the within-person centered variables.
  wstd_auth01W10 ~ c(a1, a1)*wstd_auth01W7 + c(b1, b1)*wstd_eu_intW7
  wstd_eu_intW10 ~ c(c1, c1)*wstd_auth01W7 + c(d1, d1)*wstd_eu_intW7
  wstd_auth01W11 ~ c(a2, a2)*wstd_auth01W10 + c(b2, b2)*wstd_eu_intW10
  wstd_eu_intW11 ~ c(c2, c2)*wstd_auth01W10 + c(d2, d2)*wstd_eu_intW10
  wstd_auth01W14 ~ c(a3, a3)*wstd_auth01W11 + c(b3, b3)*wstd_eu_intW11
  wstd_eu_intW14 ~ c(c3, c3)*wstd_auth01W11 + c(d3, d3)*wstd_eu_intW11

  # Estimate the covariance between the within-person centered variables at the first wave. 
  wstd_auth01W7 ~~ wstd_eu_intW7 # Covariance
  
  # Estimate the covariances between the residuals of the within-person centered variables (the innovations).
  wstd_auth01W10 ~~ wstd_eu_intW10
  wstd_auth01W11 ~~ wstd_eu_intW11
  wstd_auth01W14 ~~ wstd_eu_intW14
  
  # Fix the variance and covariance of the random intercepts to 0
  RIstd_auth01W ~~ 0*RIstd_auth01W
  RIstd_eu_intW ~~ 0*RIstd_eu_intW
  RIstd_auth01W ~~ 0*RIstd_eu_intW

  # Estimate the (residual) variance of the within-person centered variables.
  wstd_auth01W7 ~~ wstd_auth01W7 # Variances
  wstd_eu_intW7 ~~ wstd_eu_intW7 
  wstd_auth01W10 ~~ wstd_auth01W10 # Residual variances
  wstd_eu_intW10 ~~ wstd_eu_intW10 
  wstd_auth01W11 ~~ wstd_auth01W11
  wstd_eu_intW11 ~~ wstd_eu_intW11 
  wstd_auth01W14 ~~ wstd_auth01W14 
  wstd_eu_intW14 ~~ wstd_eu_intW14 
  
  # Fix error variance of indicators to (1 - alpha)*var
std_auth01W10 ~~ 0.0058*std_auth01W10
std_auth01W11 ~~ 0.002*std_auth01W11
std_auth01W14 ~~ 0.00725*std_auth01W14
std_eu_intW10 ~~ 0.0028*std_eu_intW10
std_eu_intW11 ~~ 0.00194*std_eu_intW11
std_eu_intW14 ~~ 0*std_eu_intW14
'

std_eu_ethC_CLPM.fit <- lavaan(std_eu_ethC_CLPM, 
                          estimator = "MLR",
                          data = ethnic_dat,
                          group = "ethnic_group",
                          missing = 'ML', 
                          meanstructure = T, 
                          int.ov.free = T) 
summary(std_eu_ethC_CLPM.fit, standardized = T)

anova(std_eu_ethC_CLPM.fit, std_eu_ethU_CLPM.fit)

#### Multiple-group CLPMs: Ethnicity - immigration ####

ethnic_dat_white <- read.dta13("C:/Users/justi/OneDrive/Postgraduate/PhD/Non-phd research/Pavlos Authoritarianism study/R&R - Pol Behaviour/Replication/RICLPM data_ethnicity_white.dta")

ethnic_dat_nonwhite <- read.dta13("C:/Users/justi/OneDrive/Postgraduate/PhD/Non-phd research/Pavlos Authoritarianism study/R&R - Pol Behaviour/Replication/RICLPM data_ethnicity_nonwhite.dta")

# White

std_immig_E_CLPM_W <- '
 # Create between components (random intercepts)
  RIstd_auth01W  =~ 1*std_auth01W7 + 1*std_auth01W10 + 1*std_auth01W11 + 1*std_auth01W14 
  RIim_prefW =~ 1*std_im_pref_2W7 + 1*std_im_pref_2W10 + 1*std_im_pref_2W11 + 1*std_im_pref_2W14 
  
  # Create within-person centered variables
  wstd_auth01W7 =~ NA*std_auth01W7
  wstd_auth01W10 =~ NA*std_auth01W10
  wstd_auth01W11 =~ NA*std_auth01W11 
  wstd_auth01W14 =~ NA*std_auth01W14

  wim_prefW7 =~ NA*std_im_pref_2W7
  wim_prefW10 =~ NA*std_im_pref_2W10
  wim_prefW11 =~ NA*std_im_pref_2W11
  wim_prefW14 =~ NA*std_im_pref_2W14

  # Estimate the lagged effects between the within-person centered variables.
  wstd_auth01W10 ~ a*wstd_auth01W7 + b*wim_prefW7
  wim_prefW10 ~ c*wstd_auth01W7 + d*wim_prefW7
  wstd_auth01W11 ~ a*wstd_auth01W10 + b*wim_prefW10
  wim_prefW11 ~ c*wstd_auth01W10 + d*wim_prefW10
  wstd_auth01W14 ~ a*wstd_auth01W11 + b*wim_prefW11
  wim_prefW14 ~ c*wstd_auth01W11 + d*wim_prefW11

  # Estimate the covariance between the within-person centered variables at the first wave. 
  wstd_auth01W7 ~~ cor1*wim_prefW7 # Covariance
  
  # Estimate the covariances between the residuals of the within-person centered variables (the innovations).
  wstd_auth01W10 ~~ rcov2*wim_prefW10
  wstd_auth01W11 ~~ rcov3*wim_prefW11
  wstd_auth01W14 ~~ rcov4*wim_prefW14
  
  
  # Fix the variance and covariance of the random intercepts to 0
  RIstd_auth01W ~~ 0*RIstd_auth01W
  RIim_prefW ~~ 0*RIim_prefW
  RIstd_auth01W ~~ 0*RIim_prefW

  # Estimate the (residual) variance of the within-person centered variables.
  wstd_auth01W7 ~~ 1*wstd_auth01W7 # Variances
  wim_prefW7 ~~ 1*wim_prefW7 
  
  
  wstd_auth01W10 ~~ rvx2*wstd_auth01W10 # Residual variances
  wim_prefW10 ~~ rvy2*wim_prefW10 
  wstd_auth01W11 ~~ rvx3*wstd_auth01W11
  wim_prefW11 ~~ rvy3*wim_prefW11 
  wstd_auth01W14 ~~ rvx4*wstd_auth01W14 
  wim_prefW14 ~~ rvy4*wim_prefW14 
  
  # Fix error variance of indicators to (1 - alpha)*var
std_auth01W7 ~~ 0.02*std_auth01W7
std_auth01W10 ~~ 0.016*std_auth01W10
std_auth01W11 ~~ 0.018*std_auth01W11
std_auth01W14 ~~ 0.019*std_auth01W14
std_im_pref_2W7 ~~ 0.0147*std_im_pref_2W7
std_im_pref_2W10 ~~ 0.000155*std_im_pref_2W10
std_im_pref_2W11 ~~ 0.000147*std_im_pref_2W11
std_im_pref_2W14 ~~ 0.000512*std_im_pref_2W14

 # Compute correlations of within-components at each wave
  cor2 := a*c + b*d + a*d*cor1 + b*c*cor1 + rcov2
  cor3 := a*c + b*d + a*d*cor2 + b*c*cor2 + rcov3
  cor4 := a*c + b*d + a*d*cor3 + b*c*cor3 + rcov4
 
  # Contrain residual variances of within-components such that variance of each 
  # within-component equals 1
  rvx2 == 1 - (a*a + b*b + 2*a*b*cor1)
  rvy2 == 1 - (c*c + d*d + 2*c*d*cor1)
  rvx3 == 1 - (a*a + b*b + 2*a*b*cor2)
  rvy3 == 1 - (c*c + d*d + 2*c*d*cor2)
  rvx4 == 1 - (a*a + b*b + 2*a*b*cor3)
  rvy4 == 1 - (c*c + d*d + 2*c*d*cor3)

  '
std_immig_E_CLPM_W.fit <- lavaan(std_immig_E_CLPM_W, 
                                      data = ethnic_dat_white, 
                                      estimator = "MLR",
                                      missing = 'ML',
                                      meanstructure = T, 
                                      int.ov.free = T) 
summary(std_immig_E_CLPM_W.fit, standardized = T)

std_immig_E_CLPM_W_noEQ <- '
 # Create between components (random intercepts)
  RIstd_auth01W  =~ 1*std_auth01W7 + 1*std_auth01W10 + 1*std_auth01W11 + 1*std_auth01W14 
  RIim_prefW =~ 1*std_im_pref_2W7 + 1*std_im_pref_2W10 + 1*std_im_pref_2W11 + 1*std_im_pref_2W14 
  
  # Create within-person centered variables
  wstd_auth01W7 =~ 1*std_auth01W7
  wstd_auth01W10 =~ 1*std_auth01W10
  wstd_auth01W11 =~ 1*std_auth01W11 
  wstd_auth01W14 =~ 1*std_auth01W14

  wim_prefW7 =~ 1*std_im_pref_2W7
  wim_prefW10 =~ 1*std_im_pref_2W10
  wim_prefW11 =~ 1*std_im_pref_2W11
  wim_prefW14 =~ 1*std_im_pref_2W14

  # Estimate the lagged effects between the within-person centered variables.
  wstd_auth01W10 + wim_prefW10 ~ wstd_auth01W7 + wim_prefW7
  wstd_auth01W11 + wim_prefW11 ~ wstd_auth01W10 + wim_prefW10
  wstd_auth01W14 + wim_prefW14 ~ wstd_auth01W11 + wim_prefW11

  # Estimate the covariance between the within-person centered variables at the first wave. 
  wstd_auth01W7 ~~ wim_prefW7 # Covariance
  
  # Estimate the covariances between the residuals of the within-person centered variables (the innovations).
  wstd_auth01W10 ~~ wim_prefW10
  wstd_auth01W11 ~~ wim_prefW11
  wstd_auth01W14 ~~ wim_prefW14
  
  
  # Fix the variance and covariance of the random intercepts to 0
  RIstd_auth01W ~~ 0*RIstd_auth01W
  RIim_prefW ~~ 0*RIim_prefW
  RIstd_auth01W ~~ 0*RIim_prefW

  # Estimate the (residual) variance of the within-person centered variables.
  wstd_auth01W7 ~~ wstd_auth01W7 # Variances
  wim_prefW7 ~~ wim_prefW7 
  wstd_auth01W10 ~~ wstd_auth01W10 # Residual variances
  wim_prefW10 ~~ wim_prefW10 
  wstd_auth01W11 ~~ wstd_auth01W11
  wim_prefW11 ~~ wim_prefW11 
  wstd_auth01W14 ~~ wstd_auth01W14 
  wim_prefW14 ~~ wim_prefW14 
  
  # Fix error variance of indicators to (1 - alpha)*var
std_auth01W7 ~~ 0.02*std_auth01W7
std_auth01W10 ~~ 0.016*std_auth01W10
std_auth01W11 ~~ 0.018*std_auth01W11
std_auth01W14 ~~ 0.019*std_auth01W14
std_im_pref_2W7 ~~ 0.0147*std_im_pref_2W7
std_im_pref_2W10 ~~ 0.000155*std_im_pref_2W10
std_im_pref_2W11 ~~ 0.000147*std_im_pref_2W11
std_im_pref_2W14 ~~ 0.000512*std_im_pref_2W14

  '
std_immig_E_CLPM_W_noEQ.fit <- lavaan(std_immig_E_CLPM_W_noEQ, 
                                  data = ethnic_dat_white, 
                                  estimator = "MLR",
                                  missing = 'ML',
                                  meanstructure = T, 
                                  int.ov.free = T) 
summary(std_immig_E_CLPM_W_noEQ.fit, standardized = T)

# Non-white 

std_immig_E_CLPM_NW <- '
 # Create between components (random intercepts)
  RIstd_auth01W  =~ 1*std_auth01W7 + 1*std_auth01W10 + 1*std_auth01W11 + 1*std_auth01W14 
  RIim_prefW =~ 1*std_im_pref_2W7 + 1*std_im_pref_2W10 + 1*std_im_pref_2W11 + 1*std_im_pref_2W14 
  
  # Create within-person centered variables
  wstd_auth01W7 =~ NA*std_auth01W7
  wstd_auth01W10 =~ NA*std_auth01W10
  wstd_auth01W11 =~ NA*std_auth01W11 
  wstd_auth01W14 =~ NA*std_auth01W14

  wim_prefW7 =~ NA*std_im_pref_2W7
  wim_prefW10 =~ NA*std_im_pref_2W10
  wim_prefW11 =~ NA*std_im_pref_2W11
  wim_prefW14 =~ NA*std_im_pref_2W14

  # Estimate the lagged effects between the within-person centered variables.
  wstd_auth01W10 ~ a*wstd_auth01W7 + b*wim_prefW7
  wim_prefW10 ~ c*wstd_auth01W7 + d*wim_prefW7
  wstd_auth01W11 ~ a*wstd_auth01W10 + b*wim_prefW10
  wim_prefW11 ~ c*wstd_auth01W10 + d*wim_prefW10
  wstd_auth01W14 ~ a*wstd_auth01W11 + b*wim_prefW11
  wim_prefW14 ~ c*wstd_auth01W11 + d*wim_prefW11

  # Estimate the covariance between the within-person centered variables at the first wave. 
  wstd_auth01W7 ~~ cor1*wim_prefW7 # Covariance
  
  # Estimate the covariances between the residuals of the within-person centered variables (the innovations).
  wstd_auth01W10 ~~ rcov2*wim_prefW10
  wstd_auth01W11 ~~ rcov3*wim_prefW11
  wstd_auth01W14 ~~ rcov4*wim_prefW14
  
  
  # Fix the variance and covariance of the random intercepts to 0
  RIstd_auth01W ~~ 0*RIstd_auth01W
  RIim_prefW ~~ 0*RIim_prefW
  RIstd_auth01W ~~ 0*RIim_prefW

  # Estimate the (residual) variance of the within-person centered variables.
  wstd_auth01W7 ~~ 1*wstd_auth01W7 # Variances
  wim_prefW7 ~~ 1*wim_prefW7 
  
  
  wstd_auth01W10 ~~ rvx2*wstd_auth01W10 # Residual variances
  wim_prefW10 ~~ rvy2*wim_prefW10 
  wstd_auth01W11 ~~ rvx3*wstd_auth01W11
  wim_prefW11 ~~ rvy3*wim_prefW11 
  wstd_auth01W14 ~~ rvx4*wstd_auth01W14 
  wim_prefW14 ~~ rvy4*wim_prefW14 
  
  # Fix error variance of indicators to (1 - alpha)*var
std_auth01W7 ~~ 0.02*std_auth01W7
std_auth01W10 ~~ 0.016*std_auth01W10
std_auth01W11 ~~ 0.018*std_auth01W11
std_auth01W14 ~~ 0.019*std_auth01W14
std_im_pref_2W7 ~~ 0.0147*std_im_pref_2W7
std_im_pref_2W10 ~~ 0.000155*std_im_pref_2W10
std_im_pref_2W11 ~~ 0.000147*std_im_pref_2W11
std_im_pref_2W14 ~~ 0.000512*std_im_pref_2W14

 # Compute correlations of within-components at each wave
  cor2 := a*c + b*d + a*d*cor1 + b*c*cor1 + rcov2
  cor3 := a*c + b*d + a*d*cor2 + b*c*cor2 + rcov3
  cor4 := a*c + b*d + a*d*cor3 + b*c*cor3 + rcov4
 
  # Contrain residual variances of within-components such that variance of each 
  # within-component equals 1
  rvx2 == 1 - (a*a + b*b + 2*a*b*cor1)
  rvy2 == 1 - (c*c + d*d + 2*c*d*cor1)
  rvx3 == 1 - (a*a + b*b + 2*a*b*cor2)
  rvy3 == 1 - (c*c + d*d + 2*c*d*cor2)
  rvx4 == 1 - (a*a + b*b + 2*a*b*cor3)
  rvy4 == 1 - (c*c + d*d + 2*c*d*cor3)

  '
std_immig_E_CLPM_NW.fit <- lavaan(std_immig_E_CLPM_NW, 
                                 data = ethnic_dat_nonwhite, 
                                 estimator = "MLR",
                                 missing = 'ML',
                                 meanstructure = T, 
                                 int.ov.free = T) 
summary(std_immig_E_CLPM_NW.fit, standardized = T)

std_immig_E_CLPM_NW_noEQ <- '
 # Create between components (random intercepts)
  RIstd_auth01W  =~ 1*std_auth01W7 + 1*std_auth01W10 + 1*std_auth01W11 + 1*std_auth01W14 
  RIim_prefW =~ 1*std_im_pref_2W7 + 1*std_im_pref_2W10 + 1*std_im_pref_2W11 + 1*std_im_pref_2W14 
  
  # Create within-person centered variables
  wstd_auth01W7 =~ 1*std_auth01W7
  wstd_auth01W10 =~ 1*std_auth01W10
  wstd_auth01W11 =~ 1*std_auth01W11 
  wstd_auth01W14 =~ 1*std_auth01W14

  wim_prefW7 =~ 1*std_im_pref_2W7
  wim_prefW10 =~ 1*std_im_pref_2W10
  wim_prefW11 =~ 1*std_im_pref_2W11
  wim_prefW14 =~ 1*std_im_pref_2W14

  # Estimate the lagged effects between the within-person centered variables.
  wstd_auth01W10 + wim_prefW10 ~ wstd_auth01W7 + wim_prefW7
  wstd_auth01W11 + wim_prefW11 ~ wstd_auth01W10 + wim_prefW10
  wstd_auth01W14 + wim_prefW14 ~ wstd_auth01W11 + wim_prefW11

  # Estimate the covariance between the within-person centered variables at the first wave. 
  wstd_auth01W7 ~~ wim_prefW7 # Covariance
  
  # Estimate the covariances between the residuals of the within-person centered variables (the innovations).
  wstd_auth01W10 ~~ wim_prefW10
  wstd_auth01W11 ~~ wim_prefW11
  wstd_auth01W14 ~~ wim_prefW14
  
  
  # Fix the variance and covariance of the random intercepts to 0
  RIstd_auth01W ~~ 0*RIstd_auth01W
  RIim_prefW ~~ 0*RIim_prefW
  RIstd_auth01W ~~ 0*RIim_prefW

  # Estimate the (residual) variance of the within-person centered variables.
  wstd_auth01W7 ~~ wstd_auth01W7 # Variances
  wim_prefW7 ~~ wim_prefW7 
  wstd_auth01W10 ~~ wstd_auth01W10 # Residual variances
  wim_prefW10 ~~ wim_prefW10 
  wstd_auth01W11 ~~ wstd_auth01W11
  wim_prefW11 ~~ wim_prefW11 
  wstd_auth01W14 ~~ wstd_auth01W14 
  wim_prefW14 ~~ wim_prefW14 
  
  # Fix error variance of indicators to (1 - alpha)*var
std_auth01W7 ~~ 0.02*std_auth01W7
std_auth01W10 ~~ 0.016*std_auth01W10
std_auth01W11 ~~ 0.018*std_auth01W11
std_auth01W14 ~~ 0.019*std_auth01W14
std_im_pref_2W7 ~~ 0.0147*std_im_pref_2W7
std_im_pref_2W10 ~~ 0.000155*std_im_pref_2W10
std_im_pref_2W11 ~~ 0.000147*std_im_pref_2W11
std_im_pref_2W14 ~~ 0.000512*std_im_pref_2W14

  '
std_immig_E_CLPM_NW_noEQ.fit <- lavaan(std_immig_E_CLPM_NW_noEQ, 
                                      data = ethnic_dat_nonwhite, 
                                      estimator = "MLR",
                                      missing = 'ML',
                                      meanstructure = T, 
                                      int.ov.free = T) 
summary(std_immig_E_CLPM_NW_noEQ.fit, standardized = T)

# Model fit 

fitmeasures(std_immig_E_CLPM_W.fit)
fitmeasures(std_immig_E_CLPM_W_noEQ.fit)
fitmeasures(std_immig_E_CLPM_NW.fit)
fitmeasures(std_immig_E_CLPM_NW_noEQ.fit)

# Results

standardizedsolution(std_immig_E_CLPM_W.fit)
standardizedsolution(std_immig_E_CLPM_W_noEQ.fit)
standardizedsolution(std_immig_E_CLPM_NW.fit)
standardizedsolution(std_immig_E_CLPM_NW_noEQ.fit)

## Covariates

# White 

std_immig_E_CLPM_W_cov <- '
 # Create between components (random intercepts)
  RIstd_auth01W  =~ 1*std_auth01W7 + 1*std_auth01W10 + 1*std_auth01W11 + 1*std_auth01W14 
  RIim_prefW =~ 1*std_im_pref_2W7 + 1*std_im_pref_2W10 + 1*std_im_pref_2W11 + 1*std_im_pref_2W14 
  
  # Create within-person centered variables
  wstd_auth01W7 =~ NA*std_auth01W7
  wstd_auth01W10 =~ NA*std_auth01W10
  wstd_auth01W11 =~ NA*std_auth01W11 
  wstd_auth01W14 =~ NA*std_auth01W14

  wim_prefW7 =~ NA*std_im_pref_2W7
  wim_prefW10 =~ NA*std_im_pref_2W10
  wim_prefW11 =~ NA*std_im_pref_2W11
  wim_prefW14 =~ NA*std_im_pref_2W14

  # Estimate the lagged effects between the within-person centered variables.
  wstd_auth01W10 ~ a*wstd_auth01W7 + b*wim_prefW7
  wim_prefW10 ~ c*wstd_auth01W7 + d*wim_prefW7
  wstd_auth01W11 ~ a*wstd_auth01W10 + b*wim_prefW10
  wim_prefW11 ~ c*wstd_auth01W10 + d*wim_prefW10
  wstd_auth01W14 ~ a*wstd_auth01W11 + b*wim_prefW11
  wim_prefW14 ~ c*wstd_auth01W11 + d*wim_prefW11

  # Estimate the covariance between the within-person centered variables at the first wave. 
  wstd_auth01W7 ~~ cor1*wim_prefW7 # Covariance
  
  # Estimate the covariances between the residuals of the within-person centered variables (the innovations).
  wstd_auth01W10 ~~ rcov2*wim_prefW10
  wstd_auth01W11 ~~ rcov3*wim_prefW11
  wstd_auth01W14 ~~ rcov4*wim_prefW14
  
  
  # Fix the variance and covariance of the random intercepts to 0
  RIstd_auth01W ~~ 0*RIstd_auth01W
  RIim_prefW ~~ 0*RIim_prefW
  RIstd_auth01W ~~ 0*RIim_prefW

  # Estimate the (residual) variance of the within-person centered variables.
  wstd_auth01W7 ~~ 1*wstd_auth01W7 # Variances
  wim_prefW7 ~~ 1*wim_prefW7 
  
  
  wstd_auth01W10 ~~ rvx2*wstd_auth01W10 # Residual variances
  wim_prefW10 ~~ rvy2*wim_prefW10 
  wstd_auth01W11 ~~ rvx3*wstd_auth01W11
  wim_prefW11 ~~ rvy3*wim_prefW11 
  wstd_auth01W14 ~~ rvx4*wstd_auth01W14 
  wim_prefW14 ~~ rvy4*wim_prefW14 
  
  # Fix error variance of indicators to (1 - alpha)*var
std_auth01W7 ~~ 0.02*std_auth01W7
std_auth01W10 ~~ 0.016*std_auth01W10
std_auth01W11 ~~ 0.018*std_auth01W11
std_auth01W14 ~~ 0.019*std_auth01W14
std_im_pref_2W7 ~~ 0.0147*std_im_pref_2W7
std_im_pref_2W10 ~~ 0.000155*std_im_pref_2W10
std_im_pref_2W11 ~~ 0.000147*std_im_pref_2W11
std_im_pref_2W14 ~~ 0.000512*std_im_pref_2W14

 # Compute correlations of within-components at each wave
  cor2 := a*c + b*d + a*d*cor1 + b*c*cor1 + rcov2
  cor3 := a*c + b*d + a*d*cor2 + b*c*cor2 + rcov3
  cor4 := a*c + b*d + a*d*cor3 + b*c*cor3 + rcov4
 
  # Contrain residual variances of within-components such that variance of each 
  # within-component equals 1
  rvx2 == 1 - (a*a + b*b + 2*a*b*cor1)
  rvy2 == 1 - (c*c + d*d + 2*c*d*cor1)
  rvx3 == 1 - (a*a + b*b + 2*a*b*cor2)
  rvy3 == 1 - (c*c + d*d + 2*c*d*cor2)
  rvx4 == 1 - (a*a + b*b + 2*a*b*cor3)
  rvy4 == 1 - (c*c + d*d + 2*c*d*cor3)
  
   # incorporate tvc - age
wstd_auth01W7 + wim_prefW7 ~ ageW7
wstd_auth01W10 + wim_prefW10 ~ ageW10
wstd_auth01W11 + wim_prefW11 ~ ageW11
wstd_auth01W14 + wim_prefW14 ~ ageW14

ageW7 ~~ ageW7
ageW10 ~~ ageW10
ageW11 ~~ ageW11
ageW14 ~~ ageW14

ageW7 ~~ ageW10 + ageW11 + ageW14
ageW10 ~~ ageW11 + ageW14
ageW11 ~~ ageW14

# incorporate tvc – social grade
wstd_auth01W7 + wim_prefW7 ~ p_socgradeW7
wstd_auth01W10 + wim_prefW10 ~ p_socgradeW10
wstd_auth01W11 + wim_prefW11 ~ p_socgradeW11
wstd_auth01W14 + wim_prefW14 ~ p_socgradeW14

p_socgradeW7 ~~ p_socgradeW7
p_socgradeW10 ~~ p_socgradeW10
p_socgradeW11 ~~ p_socgradeW11
p_socgradeW14 ~~ p_socgradeW14

p_socgradeW7 ~~ p_socgradeW10 + p_socgradeW11 + p_socgradeW14
p_socgradeW10 ~~ p_socgradeW11 + p_socgradeW14
p_socgradeW11 ~~ p_socgradeW14

# incorporate tvc – education
wstd_auth01W7 + wim_prefW7 ~ higheduc7
wstd_auth01W10 + wim_prefW10 ~ higheduc10
wstd_auth01W11 + wim_prefW11 ~ higheduc11
wstd_auth01W14 + wim_prefW14 ~ higheduc14

higheduc7 ~~ higheduc7
higheduc10 ~~ higheduc10
higheduc11 ~~ higheduc11
higheduc14 ~~ higheduc14

higheduc7 ~~ higheduc10 + higheduc11 + higheduc14
higheduc10 ~~ higheduc11 + higheduc14
higheduc11 ~~ higheduc14

# incorporate tvc – income
wstd_auth01W7 + wim_prefW7 ~ incomerec7
wstd_auth01W10 + wim_prefW10 ~ incomerec10
wstd_auth01W11 + wim_prefW11 ~ incomerec11
wstd_auth01W14 + wim_prefW14 ~ incomerec14

incomerec7 ~~ incomerec7
incomerec10 ~~ incomerec10
incomerec11 ~~ incomerec11
incomerec14 ~~ incomerec14

incomerec7 ~~ incomerec10 + incomerec11 + incomerec14
incomerec10 ~~ incomerec11 + incomerec14
incomerec11 ~~ incomerec14

  '
std_immig_E_CLPM_W_cov.fit <- lavaan(std_immig_E_CLPM_W_cov, 
                                 data = ethnic_dat_white, 
                                 estimator = "MLR",
                                 missing = 'ML',
                                 meanstructure = T, 
                                 int.ov.free = T) 
summary(std_immig_E_CLPM_W_cov.fit, standardized = T)

std_immig_E_CLPM_W_noEQ_cov <- '
 # Create between components (random intercepts)
  RIstd_auth01W  =~ 1*std_auth01W7 + 1*std_auth01W10 + 1*std_auth01W11 + 1*std_auth01W14 
  RIim_prefW =~ 1*std_im_pref_2W7 + 1*std_im_pref_2W10 + 1*std_im_pref_2W11 + 1*std_im_pref_2W14 
  
  # Create within-person centered variables
  wstd_auth01W7 =~ 1*std_auth01W7
  wstd_auth01W10 =~ 1*std_auth01W10
  wstd_auth01W11 =~ 1*std_auth01W11 
  wstd_auth01W14 =~ 1*std_auth01W14

  wim_prefW7 =~ 1*std_im_pref_2W7
  wim_prefW10 =~ 1*std_im_pref_2W10
  wim_prefW11 =~ 1*std_im_pref_2W11
  wim_prefW14 =~ 1*std_im_pref_2W14

  # Estimate the lagged effects between the within-person centered variables.
  wstd_auth01W10 + wim_prefW10 ~ wstd_auth01W7 + wim_prefW7
  wstd_auth01W11 + wim_prefW11 ~ wstd_auth01W10 + wim_prefW10
  wstd_auth01W14 + wim_prefW14 ~ wstd_auth01W11 + wim_prefW11

  # Estimate the covariance between the within-person centered variables at the first wave. 
  wstd_auth01W7 ~~ wim_prefW7 # Covariance
  
  # Estimate the covariances between the residuals of the within-person centered variables (the innovations).
  wstd_auth01W10 ~~ wim_prefW10
  wstd_auth01W11 ~~ wim_prefW11
  wstd_auth01W14 ~~ wim_prefW14
  
  
  # Fix the variance and covariance of the random intercepts to 0
  RIstd_auth01W ~~ 0*RIstd_auth01W
  RIim_prefW ~~ 0*RIim_prefW
  RIstd_auth01W ~~ 0*RIim_prefW

  # Estimate the (residual) variance of the within-person centered variables.
  wstd_auth01W7 ~~ wstd_auth01W7 # Variances
  wim_prefW7 ~~ wim_prefW7 
  wstd_auth01W10 ~~ wstd_auth01W10 # Residual variances
  wim_prefW10 ~~ wim_prefW10 
  wstd_auth01W11 ~~ wstd_auth01W11
  wim_prefW11 ~~ wim_prefW11 
  wstd_auth01W14 ~~ wstd_auth01W14 
  wim_prefW14 ~~ wim_prefW14 
  
  # Fix error variance of indicators to (1 - alpha)*var
std_auth01W7 ~~ 0.02*std_auth01W7
std_auth01W10 ~~ 0.016*std_auth01W10
std_auth01W11 ~~ 0.018*std_auth01W11
std_auth01W14 ~~ 0.019*std_auth01W14
std_im_pref_2W7 ~~ 0.0147*std_im_pref_2W7
std_im_pref_2W10 ~~ 0.000155*std_im_pref_2W10
std_im_pref_2W11 ~~ 0.000147*std_im_pref_2W11
std_im_pref_2W14 ~~ 0.000512*std_im_pref_2W14

 # incorporate tvc - age
wstd_auth01W7 + wim_prefW7 ~ ageW7
wstd_auth01W10 + wim_prefW10 ~ ageW10
wstd_auth01W11 + wim_prefW11 ~ ageW11
wstd_auth01W14 + wim_prefW14 ~ ageW14

ageW7 ~~ ageW7
ageW10 ~~ ageW10
ageW11 ~~ ageW11
ageW14 ~~ ageW14

ageW7 ~~ ageW10 + ageW11 + ageW14
ageW10 ~~ ageW11 + ageW14
ageW11 ~~ ageW14

# incorporate tvc – social grade
wstd_auth01W7 + wim_prefW7 ~ p_socgradeW7
wstd_auth01W10 + wim_prefW10 ~ p_socgradeW10
wstd_auth01W11 + wim_prefW11 ~ p_socgradeW11
wstd_auth01W14 + wim_prefW14 ~ p_socgradeW14

p_socgradeW7 ~~ p_socgradeW7
p_socgradeW10 ~~ p_socgradeW10
p_socgradeW11 ~~ p_socgradeW11
p_socgradeW14 ~~ p_socgradeW14

p_socgradeW7 ~~ p_socgradeW10 + p_socgradeW11 + p_socgradeW14
p_socgradeW10 ~~ p_socgradeW11 + p_socgradeW14
p_socgradeW11 ~~ p_socgradeW14

# incorporate tvc – education
wstd_auth01W7 + wim_prefW7 ~ higheduc7
wstd_auth01W10 + wim_prefW10 ~ higheduc10
wstd_auth01W11 + wim_prefW11 ~ higheduc11
wstd_auth01W14 + wim_prefW14 ~ higheduc14

higheduc7 ~~ higheduc7
higheduc10 ~~ higheduc10
higheduc11 ~~ higheduc11
higheduc14 ~~ higheduc14

higheduc7 ~~ higheduc10 + higheduc11 + higheduc14
higheduc10 ~~ higheduc11 + higheduc14
higheduc11 ~~ higheduc14

# incorporate tvc – income
wstd_auth01W7 + wim_prefW7 ~ incomerec7
wstd_auth01W10 + wim_prefW10 ~ incomerec10
wstd_auth01W11 + wim_prefW11 ~ incomerec11
wstd_auth01W14 + wim_prefW14 ~ incomerec14

incomerec7 ~~ incomerec7
incomerec10 ~~ incomerec10
incomerec11 ~~ incomerec11
incomerec14 ~~ incomerec14

incomerec7 ~~ incomerec10 + incomerec11 + incomerec14
incomerec10 ~~ incomerec11 + incomerec14
incomerec11 ~~ incomerec14

  '
std_immig_E_CLPM_W_noEQ_cov.fit <- lavaan(std_immig_E_CLPM_W_noEQ_cov, 
                                      data = ethnic_dat_white, 
                                      estimator = "MLR",
                                      missing = 'ML',
                                      meanstructure = T, 
                                      int.ov.free = T) 
summary(std_immig_E_CLPM_W_noEQ_cov.fit, standardized = T)

# Non-white 

std_immig_E_CLPM_NW_cov <- '
 # Create between components (random intercepts)
  RIstd_auth01W  =~ 1*std_auth01W7 + 1*std_auth01W10 + 1*std_auth01W11 + 1*std_auth01W14 
  RIim_prefW =~ 1*std_im_pref_2W7 + 1*std_im_pref_2W10 + 1*std_im_pref_2W11 + 1*std_im_pref_2W14 
  
  # Create within-person centered variables
  wstd_auth01W7 =~ NA*std_auth01W7
  wstd_auth01W10 =~ NA*std_auth01W10
  wstd_auth01W11 =~ NA*std_auth01W11 
  wstd_auth01W14 =~ NA*std_auth01W14

  wim_prefW7 =~ NA*std_im_pref_2W7
  wim_prefW10 =~ NA*std_im_pref_2W10
  wim_prefW11 =~ NA*std_im_pref_2W11
  wim_prefW14 =~ NA*std_im_pref_2W14

  # Estimate the lagged effects between the within-person centered variables.
  wstd_auth01W10 ~ a*wstd_auth01W7 + b*wim_prefW7
  wim_prefW10 ~ c*wstd_auth01W7 + d*wim_prefW7
  wstd_auth01W11 ~ a*wstd_auth01W10 + b*wim_prefW10
  wim_prefW11 ~ c*wstd_auth01W10 + d*wim_prefW10
  wstd_auth01W14 ~ a*wstd_auth01W11 + b*wim_prefW11
  wim_prefW14 ~ c*wstd_auth01W11 + d*wim_prefW11

  # Estimate the covariance between the within-person centered variables at the first wave. 
  wstd_auth01W7 ~~ cor1*wim_prefW7 # Covariance
  
  # Estimate the covariances between the residuals of the within-person centered variables (the innovations).
  wstd_auth01W10 ~~ rcov2*wim_prefW10
  wstd_auth01W11 ~~ rcov3*wim_prefW11
  wstd_auth01W14 ~~ rcov4*wim_prefW14
  
  
  # Fix the variance and covariance of the random intercepts to 0
  RIstd_auth01W ~~ 0*RIstd_auth01W
  RIim_prefW ~~ 0*RIim_prefW
  RIstd_auth01W ~~ 0*RIim_prefW

  # Estimate the (residual) variance of the within-person centered variables.
  wstd_auth01W7 ~~ 1*wstd_auth01W7 # Variances
  wim_prefW7 ~~ 1*wim_prefW7 
  
  
  wstd_auth01W10 ~~ rvx2*wstd_auth01W10 # Residual variances
  wim_prefW10 ~~ rvy2*wim_prefW10 
  wstd_auth01W11 ~~ rvx3*wstd_auth01W11
  wim_prefW11 ~~ rvy3*wim_prefW11 
  wstd_auth01W14 ~~ rvx4*wstd_auth01W14 
  wim_prefW14 ~~ rvy4*wim_prefW14 
  
  # Fix error variance of indicators to (1 - alpha)*var
std_auth01W7 ~~ 0.02*std_auth01W7
std_auth01W10 ~~ 0.016*std_auth01W10
std_auth01W11 ~~ 0.018*std_auth01W11
std_auth01W14 ~~ 0.019*std_auth01W14
std_im_pref_2W7 ~~ 0.0147*std_im_pref_2W7
std_im_pref_2W10 ~~ 0.000155*std_im_pref_2W10
std_im_pref_2W11 ~~ 0.000147*std_im_pref_2W11
std_im_pref_2W14 ~~ 0.000512*std_im_pref_2W14

 # Compute correlations of within-components at each wave
  cor2 := a*c + b*d + a*d*cor1 + b*c*cor1 + rcov2
  cor3 := a*c + b*d + a*d*cor2 + b*c*cor2 + rcov3
  cor4 := a*c + b*d + a*d*cor3 + b*c*cor3 + rcov4
 
  # Contrain residual variances of within-components such that variance of each 
  # within-component equals 1
  rvx2 == 1 - (a*a + b*b + 2*a*b*cor1)
  rvy2 == 1 - (c*c + d*d + 2*c*d*cor1)
  rvx3 == 1 - (a*a + b*b + 2*a*b*cor2)
  rvy3 == 1 - (c*c + d*d + 2*c*d*cor2)
  rvx4 == 1 - (a*a + b*b + 2*a*b*cor3)
  rvy4 == 1 - (c*c + d*d + 2*c*d*cor3)
  
   # incorporate tvc - age
wstd_auth01W7 + wim_prefW7 ~ ageW7
wstd_auth01W10 + wim_prefW10 ~ ageW10
wstd_auth01W11 + wim_prefW11 ~ ageW11
wstd_auth01W14 + wim_prefW14 ~ ageW14

ageW7 ~~ ageW7
ageW10 ~~ ageW10
ageW11 ~~ ageW11
ageW14 ~~ ageW14

ageW7 ~~ ageW10 + ageW11 + ageW14
ageW10 ~~ ageW11 + ageW14
ageW11 ~~ ageW14


# incorporate tvc – social grade
wstd_auth01W7 + wim_prefW7 ~ p_socgradeW7
wstd_auth01W10 + wim_prefW10 ~ p_socgradeW10
wstd_auth01W11 + wim_prefW11 ~ p_socgradeW11
wstd_auth01W14 + wim_prefW14 ~ p_socgradeW14

p_socgradeW7 ~~ p_socgradeW7
p_socgradeW10 ~~ p_socgradeW10
p_socgradeW11 ~~ p_socgradeW11
p_socgradeW14 ~~ p_socgradeW14

p_socgradeW7 ~~ p_socgradeW10 + p_socgradeW11 + p_socgradeW14
p_socgradeW10 ~~ p_socgradeW11 + p_socgradeW14
p_socgradeW11 ~~ p_socgradeW14

# incorporate tvc – education
wstd_auth01W7 + wim_prefW7 ~ higheduc7
wstd_auth01W10 + wim_prefW10 ~ higheduc10
wstd_auth01W11 + wim_prefW11 ~ higheduc11
wstd_auth01W14 + wim_prefW14 ~ higheduc14

higheduc7 ~~ higheduc7
higheduc10 ~~ higheduc10
higheduc11 ~~ higheduc11
higheduc14 ~~ higheduc14

higheduc7 ~~ higheduc10 + higheduc11 + higheduc14
higheduc10 ~~ higheduc11 + higheduc14
higheduc11 ~~ higheduc14

# incorporate tvc – income
wstd_auth01W7 + wim_prefW7 ~ incomerec7
wstd_auth01W10 + wim_prefW10 ~ incomerec10
wstd_auth01W11 + wim_prefW11 ~ incomerec11
wstd_auth01W14 + wim_prefW14 ~ incomerec14

incomerec7 ~~ incomerec7
incomerec10 ~~ incomerec10
incomerec11 ~~ incomerec11
incomerec14 ~~ incomerec14

incomerec7 ~~ incomerec10 + incomerec11 + incomerec14
incomerec10 ~~ incomerec11 + incomerec14
incomerec11 ~~ incomerec14

  '
std_immig_E_CLPM_NW_cov.fit <- lavaan(std_immig_E_CLPM_NW_cov, 
                                  data = ethnic_dat_nonwhite, 
                                  estimator = "MLR",
                                  missing = 'ML',
                                  meanstructure = T, 
                                  int.ov.free = T) 
summary(std_immig_E_CLPM_NW_cov.fit, standardized = T)

std_immig_E_CLPM_NW_noEQ_cov <- '
 # Create between components (random intercepts)
  RIstd_auth01W  =~ 1*std_auth01W7 + 1*std_auth01W10 + 1*std_auth01W11 + 1*std_auth01W14 
  RIim_prefW =~ 1*std_im_pref_2W7 + 1*std_im_pref_2W10 + 1*std_im_pref_2W11 + 1*std_im_pref_2W14 
  
  # Create within-person centered variables
  wstd_auth01W7 =~ 1*std_auth01W7
  wstd_auth01W10 =~ 1*std_auth01W10
  wstd_auth01W11 =~ 1*std_auth01W11 
  wstd_auth01W14 =~ 1*std_auth01W14

  wim_prefW7 =~ 1*std_im_pref_2W7
  wim_prefW10 =~ 1*std_im_pref_2W10
  wim_prefW11 =~ 1*std_im_pref_2W11
  wim_prefW14 =~ 1*std_im_pref_2W14

  # Estimate the lagged effects between the within-person centered variables.
  wstd_auth01W10 + wim_prefW10 ~ wstd_auth01W7 + wim_prefW7
  wstd_auth01W11 + wim_prefW11 ~ wstd_auth01W10 + wim_prefW10
  wstd_auth01W14 + wim_prefW14 ~ wstd_auth01W11 + wim_prefW11

  # Estimate the covariance between the within-person centered variables at the first wave. 
  wstd_auth01W7 ~~ wim_prefW7 # Covariance
  
  # Estimate the covariances between the residuals of the within-person centered variables (the innovations).
  wstd_auth01W10 ~~ wim_prefW10
  wstd_auth01W11 ~~ wim_prefW11
  wstd_auth01W14 ~~ wim_prefW14
  
  
  # Fix the variance and covariance of the random intercepts to 0
  RIstd_auth01W ~~ 0*RIstd_auth01W
  RIim_prefW ~~ 0*RIim_prefW
  RIstd_auth01W ~~ 0*RIim_prefW

  # Estimate the (residual) variance of the within-person centered variables.
  wstd_auth01W7 ~~ wstd_auth01W7 # Variances
  wim_prefW7 ~~ wim_prefW7 
  wstd_auth01W10 ~~ wstd_auth01W10 # Residual variances
  wim_prefW10 ~~ wim_prefW10 
  wstd_auth01W11 ~~ wstd_auth01W11
  wim_prefW11 ~~ wim_prefW11 
  wstd_auth01W14 ~~ wstd_auth01W14 
  wim_prefW14 ~~ wim_prefW14 
  
  # Fix error variance of indicators to (1 - alpha)*var
std_auth01W7 ~~ 0.02*std_auth01W7
std_auth01W10 ~~ 0.016*std_auth01W10
std_auth01W11 ~~ 0.018*std_auth01W11
std_auth01W14 ~~ 0.019*std_auth01W14
std_im_pref_2W7 ~~ 0.0147*std_im_pref_2W7
std_im_pref_2W10 ~~ 0.000155*std_im_pref_2W10
std_im_pref_2W11 ~~ 0.000147*std_im_pref_2W11
std_im_pref_2W14 ~~ 0.000512*std_im_pref_2W14

 # incorporate tvc - age
wstd_auth01W7 + wim_prefW7 ~ ageW7
wstd_auth01W10 + wim_prefW10 ~ ageW10
wstd_auth01W11 + wim_prefW11 ~ ageW11
wstd_auth01W14 + wim_prefW14 ~ ageW14

ageW7 ~~ ageW7
ageW10 ~~ ageW10
ageW11 ~~ ageW11
ageW14 ~~ ageW14

ageW7 ~~ ageW10 + ageW11 + ageW14
ageW10 ~~ ageW11 + ageW14
ageW11 ~~ ageW14

# incorporate tvc – social grade
wstd_auth01W7 + wim_prefW7 ~ p_socgradeW7
wstd_auth01W10 + wim_prefW10 ~ p_socgradeW10
wstd_auth01W11 + wim_prefW11 ~ p_socgradeW11
wstd_auth01W14 + wim_prefW14 ~ p_socgradeW14

p_socgradeW7 ~~ p_socgradeW7
p_socgradeW10 ~~ p_socgradeW10
p_socgradeW11 ~~ p_socgradeW11
p_socgradeW14 ~~ p_socgradeW14

p_socgradeW7 ~~ p_socgradeW10 + p_socgradeW11 + p_socgradeW14
p_socgradeW10 ~~ p_socgradeW11 + p_socgradeW14
p_socgradeW11 ~~ p_socgradeW14

# incorporate tvc – education
wstd_auth01W7 + wim_prefW7 ~ higheduc7
wstd_auth01W10 + wim_prefW10 ~ higheduc10
wstd_auth01W11 + wim_prefW11 ~ higheduc11
wstd_auth01W14 + wim_prefW14 ~ higheduc14

higheduc7 ~~ higheduc7
higheduc10 ~~ higheduc10
higheduc11 ~~ higheduc11
higheduc14 ~~ higheduc14

higheduc7 ~~ higheduc10 + higheduc11 + higheduc14
higheduc10 ~~ higheduc11 + higheduc14
higheduc11 ~~ higheduc14

# incorporate tvc – income
wstd_auth01W7 + wim_prefW7 ~ incomerec7
wstd_auth01W10 + wim_prefW10 ~ incomerec10
wstd_auth01W11 + wim_prefW11 ~ incomerec11
wstd_auth01W14 + wim_prefW14 ~ incomerec14

incomerec7 ~~ incomerec7
incomerec10 ~~ incomerec10
incomerec11 ~~ incomerec11
incomerec14 ~~ incomerec14

incomerec7 ~~ incomerec10 + incomerec11 + incomerec14
incomerec10 ~~ incomerec11 + incomerec14
incomerec11 ~~ incomerec14

  '
std_immig_E_CLPM_NW_noEQ_cov.fit <- lavaan(std_immig_E_CLPM_NW_noEQ_cov, 
                                       data = ethnic_dat_nonwhite, 
                                       estimator = "MLR",
                                       missing = 'ML',
                                       meanstructure = T, 
                                       int.ov.free = T) 
summary(std_immig_E_CLPM_NW_noEQ_cov.fit, standardized = T)

# Results 

standardizedsolution(std_immig_E_CLPM_W_cov.fit)
standardizedsolution(std_immig_E_CLPM_W_noEQ_cov.fit)

standardizedsolution(std_immig_E_CLPM_NW_cov.fit)
standardizedsolution(std_immig_E_CLPM_NW_noEQ_cov.fit)

#### Multiple-group CLPMs: Ethnicity - EU integration ####

std_eu_E_CLPM_W <- '
  # Create between components (random intercepts)
  RIstd_auth01W  =~ 1*std_auth01W7 + 1*std_auth01W10 + 1*std_auth01W11 + 1*std_auth01W14 
  RIstd_eu_intW =~ 1*std_eu_intW7 + 1*std_eu_intW10 + 1*std_eu_intW11 + 1*std_eu_intW14 
  
  # Create within-person centered variables
  wstd_auth01W7 =~ NA*std_auth01W7
  wstd_auth01W10 =~ NA*std_auth01W10
  wstd_auth01W11 =~ NA*std_auth01W11 
  wstd_auth01W14 =~ NA*std_auth01W14

  wstd_eu_intW7 =~ NA*std_eu_intW7
  wstd_eu_intW10 =~ NA*std_eu_intW10
  wstd_eu_intW11 =~ NA*std_eu_intW11
  wstd_eu_intW14 =~ NA*std_eu_intW14

  # Estimate the lagged effects between the within-person centered variables.
  wstd_auth01W10 ~ a*wstd_auth01W7 + b*wstd_eu_intW7
  wstd_eu_intW10 ~ c*wstd_auth01W7 + d*wstd_eu_intW7
  wstd_auth01W11 ~ a*wstd_auth01W10 + b*wstd_eu_intW10
  wstd_eu_intW11 ~ c*wstd_auth01W10 + d*wstd_eu_intW10
  wstd_auth01W14 ~ a*wstd_auth01W11 + b*wstd_eu_intW11
  wstd_eu_intW14 ~ c*wstd_auth01W11 + d*wstd_eu_intW11

  # Estimate the covariance between the within-person centered variables at the first wave. 
  wstd_auth01W7 ~~ cor1*wstd_eu_intW7 # Covariance
  
  # Estimate the covariances between the residuals of the within-person centered variables (the innovations).
  wstd_auth01W10 ~~ rcov2*wstd_eu_intW10
  wstd_auth01W11 ~~ rcov3*wstd_eu_intW11
  wstd_auth01W14 ~~ rcov4*wstd_eu_intW14
  
  
  # Fix the variance and covariance of the random intercepts to 0 
  RIstd_auth01W ~~ 0*RIstd_auth01W
  RIstd_eu_intW ~~ 0*RIstd_eu_intW
  RIstd_auth01W ~~ 0*RIstd_eu_intW

  # Set variances of within-components at first wave to 1
  wstd_auth01W7 ~~ 1*wstd_auth01W7 # Variances
  wstd_eu_intW7 ~~ 1*wstd_eu_intW7 
  
  wstd_auth01W10 ~~ rvx2*wstd_auth01W10 # Residual variances
  wstd_eu_intW10 ~~ rvy2*wstd_eu_intW10 
  wstd_auth01W11 ~~ rvx3*wstd_auth01W11
  wstd_eu_intW11 ~~ rvy3*wstd_eu_intW11 
  wstd_auth01W14 ~~ rvx4*wstd_auth01W14 
  wstd_eu_intW14 ~~ rvy4*wstd_eu_intW14 
  
  # Fix error variance of indicators to (1 - alpha)*var
std_auth01W10 ~~ 0.0058*std_auth01W10
std_auth01W11 ~~ 0.002*std_auth01W11
std_auth01W14 ~~ 0.00725*std_auth01W14
std_eu_intW10 ~~ 0.0028*std_eu_intW10
std_eu_intW11 ~~ 0.00194*std_eu_intW11
std_eu_intW14 ~~ 0*std_eu_intW14

 # Compute correlations of within-components at each wave
  cor2 := a*c + b*d + a*d*cor1 + b*c*cor1 + rcov2
  cor3 := a*c + b*d + a*d*cor2 + b*c*cor2 + rcov3
  cor4 := a*c + b*d + a*d*cor3 + b*c*cor3 + rcov4
 
  # Contrain residual variances of within-components such that variance of each 
  # within-component equals 1
  rvx2 == 1 - (a*a + b*b + 2*a*b*cor1)
  rvy2 == 1 - (c*c + d*d + 2*c*d*cor1)
  rvx3 == 1 - (a*a + b*b + 2*a*b*cor2)
  rvy3 == 1 - (c*c + d*d + 2*c*d*cor2)
  rvx4 == 1 - (a*a + b*b + 2*a*b*cor3)
  rvy4 == 1 - (c*c + d*d + 2*c*d*cor3)
'

std_eu_E_CLPM_W.fit <- lavaan(std_eu_E_CLPM_W, 
                            estimator = "MLR",
                            data = ethnic_dat_white,
                            missing = 'ML', 
                            meanstructure = T, 
                            int.ov.free = T) 
summary(std_eu_E_CLPM_W.fit, standardized = T)

std_eu_E_CLPM_W_noEQ <- '
  # Create between components (random intercepts)
  RIstd_auth01W  =~ 1*std_auth01W7 + 1*std_auth01W10 + 1*std_auth01W11 + 1*std_auth01W14 
  RIstd_eu_intW =~ 1*std_eu_intW7 + 1*std_eu_intW10 + 1*std_eu_intW11 + 1*std_eu_intW14 
  
  # Create within-person centered variables
  wstd_auth01W7 =~ 1*std_auth01W7
  wstd_auth01W10 =~ 1*std_auth01W10
  wstd_auth01W11 =~ 1*std_auth01W11 
  wstd_auth01W14 =~ 1*std_auth01W14

  wstd_eu_intW7 =~ 1*std_eu_intW7
  wstd_eu_intW10 =~ 1*std_eu_intW10
  wstd_eu_intW11 =~ 1*std_eu_intW11
  wstd_eu_intW14 =~ 1*std_eu_intW14

  # Estimate the lagged effects between the within-person centered variables.
  wstd_auth01W10 + wstd_eu_intW10 ~ wstd_auth01W7 + wstd_eu_intW7
  wstd_auth01W11 + wstd_eu_intW11 ~ wstd_auth01W10 + wstd_eu_intW10
  wstd_auth01W14 + wstd_eu_intW14 ~ wstd_auth01W11 + wstd_eu_intW11

  # Estimate the covariance between the within-person centered variables at the first wave. 
  wstd_auth01W7 ~~ wstd_eu_intW7 # Covariance
  
  # Estimate the covariances between the residuals of the within-person centered variables (the innovations).
  wstd_auth01W10 ~~ wstd_eu_intW10
  wstd_auth01W11 ~~ wstd_eu_intW11
  wstd_auth01W14 ~~ wstd_eu_intW14
  
  
  # Fix the variance and covariance of the random intercepts to 0 
  RIstd_auth01W ~~ 0*RIstd_auth01W
  RIstd_eu_intW ~~ 0*RIstd_eu_intW
  RIstd_auth01W ~~ 0*RIstd_eu_intW

  # Estimate the (residual) variance of the within-person centered variables.
  wstd_auth01W7 ~~ wstd_auth01W7 # Variances
  wstd_eu_intW7 ~~ wstd_eu_intW7 
  wstd_auth01W10 ~~ wstd_auth01W10 # Residual variances
  wstd_eu_intW10 ~~ wstd_eu_intW10 
  wstd_auth01W11 ~~ wstd_auth01W11
  wstd_eu_intW11 ~~ wstd_eu_intW11 
  wstd_auth01W14 ~~ wstd_auth01W14 
  wstd_eu_intW14 ~~ wstd_eu_intW14 
  
  # Fix error variance of indicators to (1 - alpha)*var
std_auth01W10 ~~ 0.0058*std_auth01W10
std_auth01W11 ~~ 0.002*std_auth01W11
std_auth01W14 ~~ 0.00725*std_auth01W14
std_eu_intW10 ~~ 0.0028*std_eu_intW10
std_eu_intW11 ~~ 0.00194*std_eu_intW11
std_eu_intW14 ~~ 0*std_eu_intW14


'

std_eu_E_CLPM_W_noEQ.fit <- lavaan(std_eu_E_CLPM_W_noEQ, 
                                 estimator = "MLR",
                                 data = ethnic_dat_white,
                                 missing = 'ML', 
                                 meanstructure = T, 
                                 int.ov.free = T) 
summary(std_eu_E_CLPM_W_noEQ.fit, standardized = T)

std_eu_E_CLPM_NW <- '
  # Create between components (random intercepts)
  RIstd_auth01W  =~ 1*std_auth01W7 + 1*std_auth01W10 + 1*std_auth01W11 + 1*std_auth01W14 
  RIstd_eu_intW =~ 1*std_eu_intW7 + 1*std_eu_intW10 + 1*std_eu_intW11 + 1*std_eu_intW14 
  
  # Create within-person centered variables
  wstd_auth01W7 =~ NA*std_auth01W7
  wstd_auth01W10 =~ NA*std_auth01W10
  wstd_auth01W11 =~ NA*std_auth01W11 
  wstd_auth01W14 =~ NA*std_auth01W14

  wstd_eu_intW7 =~ NA*std_eu_intW7
  wstd_eu_intW10 =~ NA*std_eu_intW10
  wstd_eu_intW11 =~ NA*std_eu_intW11
  wstd_eu_intW14 =~ NA*std_eu_intW14

  # Estimate the lagged effects between the within-person centered variables.
  wstd_auth01W10 ~ a*wstd_auth01W7 + b*wstd_eu_intW7
  wstd_eu_intW10 ~ c*wstd_auth01W7 + d*wstd_eu_intW7
  wstd_auth01W11 ~ a*wstd_auth01W10 + b*wstd_eu_intW10
  wstd_eu_intW11 ~ c*wstd_auth01W10 + d*wstd_eu_intW10
  wstd_auth01W14 ~ a*wstd_auth01W11 + b*wstd_eu_intW11
  wstd_eu_intW14 ~ c*wstd_auth01W11 + d*wstd_eu_intW11

  # Estimate the covariance between the within-person centered variables at the first wave. 
  wstd_auth01W7 ~~ cor1*wstd_eu_intW7 # Covariance
  
  # Estimate the covariances between the residuals of the within-person centered variables (the innovations).
  wstd_auth01W10 ~~ rcov2*wstd_eu_intW10
  wstd_auth01W11 ~~ rcov3*wstd_eu_intW11
  wstd_auth01W14 ~~ rcov4*wstd_eu_intW14
  
  
  # Fix the variance and covariance of the random intercepts to 0 
  RIstd_auth01W ~~ 0*RIstd_auth01W
  RIstd_eu_intW ~~ 0*RIstd_eu_intW
  RIstd_auth01W ~~ 0*RIstd_eu_intW

  # Set variances of within-components at first wave to 1
  wstd_auth01W7 ~~ 1*wstd_auth01W7 # Variances
  wstd_eu_intW7 ~~ 1*wstd_eu_intW7 
  
  wstd_auth01W10 ~~ rvx2*wstd_auth01W10 # Residual variances
  wstd_eu_intW10 ~~ rvy2*wstd_eu_intW10 
  wstd_auth01W11 ~~ rvx3*wstd_auth01W11
  wstd_eu_intW11 ~~ rvy3*wstd_eu_intW11 
  wstd_auth01W14 ~~ rvx4*wstd_auth01W14 
  wstd_eu_intW14 ~~ rvy4*wstd_eu_intW14 
  
  # Fix error variance of indicators to (1 - alpha)*var
std_auth01W10 ~~ 0.0058*std_auth01W10
std_auth01W11 ~~ 0.002*std_auth01W11
std_auth01W14 ~~ 0.00725*std_auth01W14
std_eu_intW10 ~~ 0.0028*std_eu_intW10
std_eu_intW11 ~~ 0.00194*std_eu_intW11
std_eu_intW14 ~~ 0*std_eu_intW14

 # Compute correlations of within-components at each wave
  cor2 := a*c + b*d + a*d*cor1 + b*c*cor1 + rcov2
  cor3 := a*c + b*d + a*d*cor2 + b*c*cor2 + rcov3
  cor4 := a*c + b*d + a*d*cor3 + b*c*cor3 + rcov4
 
  # Contrain residual variances of within-components such that variance of each 
  # within-component equals 1
  rvx2 == 1 - (a*a + b*b + 2*a*b*cor1)
  rvy2 == 1 - (c*c + d*d + 2*c*d*cor1)
  rvx3 == 1 - (a*a + b*b + 2*a*b*cor2)
  rvy3 == 1 - (c*c + d*d + 2*c*d*cor2)
  rvx4 == 1 - (a*a + b*b + 2*a*b*cor3)
  rvy4 == 1 - (c*c + d*d + 2*c*d*cor3)
'

std_eu_E_CLPM_NW.fit <- lavaan(std_eu_E_CLPM_NW, 
                              estimator = "MLR",
                              data = ethnic_dat_nonwhite,
                              missing = 'ML', 
                              meanstructure = T, 
                              int.ov.free = T) 
summary(std_eu_E_CLPM_NW.fit, standardized = T)

std_eu_E_CLPM_NW_noEQ <- '
  # Create between components (random intercepts)
  RIstd_auth01W  =~ 1*std_auth01W7 + 1*std_auth01W10 + 1*std_auth01W11 + 1*std_auth01W14 
  RIstd_eu_intW =~ 1*std_eu_intW7 + 1*std_eu_intW10 + 1*std_eu_intW11 + 1*std_eu_intW14 
  
  # Create within-person centered variables
  wstd_auth01W7 =~ 1*std_auth01W7
  wstd_auth01W10 =~ 1*std_auth01W10
  wstd_auth01W11 =~ 1*std_auth01W11 
  wstd_auth01W14 =~ 1*std_auth01W14

  wstd_eu_intW7 =~ 1*std_eu_intW7
  wstd_eu_intW10 =~ 1*std_eu_intW10
  wstd_eu_intW11 =~ 1*std_eu_intW11
  wstd_eu_intW14 =~ 1*std_eu_intW14

  # Estimate the lagged effects between the within-person centered variables.
  wstd_auth01W10 + wstd_eu_intW10 ~ wstd_auth01W7 + wstd_eu_intW7
  wstd_auth01W11 + wstd_eu_intW11 ~ wstd_auth01W10 + wstd_eu_intW10
  wstd_auth01W14 + wstd_eu_intW14 ~ wstd_auth01W11 + wstd_eu_intW11

  # Estimate the covariance between the within-person centered variables at the first wave. 
  wstd_auth01W7 ~~ wstd_eu_intW7 # Covariance
  
  # Estimate the covariances between the residuals of the within-person centered variables (the innovations).
  wstd_auth01W10 ~~ wstd_eu_intW10
  wstd_auth01W11 ~~ wstd_eu_intW11
  wstd_auth01W14 ~~ wstd_eu_intW14
  
  
  # Fix the variance and covariance of the random intercepts to 0 
  RIstd_auth01W ~~ 0*RIstd_auth01W
  RIstd_eu_intW ~~ 0*RIstd_eu_intW
  RIstd_auth01W ~~ 0*RIstd_eu_intW

  # Estimate the (residual) variance of the within-person centered variables.
  wstd_auth01W7 ~~ wstd_auth01W7 # Variances
  wstd_eu_intW7 ~~ wstd_eu_intW7 
  wstd_auth01W10 ~~ wstd_auth01W10 # Residual variances
  wstd_eu_intW10 ~~ wstd_eu_intW10 
  wstd_auth01W11 ~~ wstd_auth01W11
  wstd_eu_intW11 ~~ wstd_eu_intW11 
  wstd_auth01W14 ~~ wstd_auth01W14 
  wstd_eu_intW14 ~~ wstd_eu_intW14 
  
  # Fix error variance of indicators to (1 - alpha)*var
std_auth01W10 ~~ 0.0058*std_auth01W10
std_auth01W11 ~~ 0.002*std_auth01W11
std_auth01W14 ~~ 0.00725*std_auth01W14
std_eu_intW10 ~~ 0.0028*std_eu_intW10
std_eu_intW11 ~~ 0.00194*std_eu_intW11
std_eu_intW14 ~~ 0*std_eu_intW14


'

std_eu_E_CLPM_NW_noEQ.fit <- lavaan(std_eu_E_CLPM_NW_noEQ, 
                                   estimator = "MLR",
                                   data = ethnic_dat_nonwhite,
                                   missing = 'ML', 
                                   meanstructure = T, 
                                   int.ov.free = T) 
summary(std_eu_E_CLPM_NW_noEQ.fit, standardized = T)

# Model fit

fitmeasures(std_eu_E_CLPM_W.fit)
fitmeasures(std_eu_E_CLPM_W_noEQ.fit)
fitmeasures(std_eu_E_CLPM_NW.fit)
fitmeasures(std_eu_E_CLPM_NW_noEQ.fit)

# Results 

standardizedsolution(std_eu_E_CLPM_W.fit)
standardizedsolution(std_eu_E_CLPM_NW.fit)

standardizedsolution(std_eu_E_CLPM_W_noEQ.fit)
standardizedsolution(std_eu_E_CLPM_NW_noEQ.fit)

## Covariate models 

std_eu_E_CLPM_W_cov <- '
  # Create between components (random intercepts)
  RIstd_auth01W  =~ 1*std_auth01W7 + 1*std_auth01W10 + 1*std_auth01W11 + 1*std_auth01W14 
  RIstd_eu_intW =~ 1*std_eu_intW7 + 1*std_eu_intW10 + 1*std_eu_intW11 + 1*std_eu_intW14 
  
  # Create within-person centered variables
  wstd_auth01W7 =~ NA*std_auth01W7
  wstd_auth01W10 =~ NA*std_auth01W10
  wstd_auth01W11 =~ NA*std_auth01W11 
  wstd_auth01W14 =~ NA*std_auth01W14

  wstd_eu_intW7 =~ NA*std_eu_intW7
  wstd_eu_intW10 =~ NA*std_eu_intW10
  wstd_eu_intW11 =~ NA*std_eu_intW11
  wstd_eu_intW14 =~ NA*std_eu_intW14

  # Estimate the lagged effects between the within-person centered variables.
  wstd_auth01W10 ~ a*wstd_auth01W7 + b*wstd_eu_intW7
  wstd_eu_intW10 ~ c*wstd_auth01W7 + d*wstd_eu_intW7
  wstd_auth01W11 ~ a*wstd_auth01W10 + b*wstd_eu_intW10
  wstd_eu_intW11 ~ c*wstd_auth01W10 + d*wstd_eu_intW10
  wstd_auth01W14 ~ a*wstd_auth01W11 + b*wstd_eu_intW11
  wstd_eu_intW14 ~ c*wstd_auth01W11 + d*wstd_eu_intW11

  # Estimate the covariance between the within-person centered variables at the first wave. 
  wstd_auth01W7 ~~ cor1*wstd_eu_intW7 # Covariance
  
  # Estimate the covariances between the residuals of the within-person centered variables (the innovations).
  wstd_auth01W10 ~~ rcov2*wstd_eu_intW10
  wstd_auth01W11 ~~ rcov3*wstd_eu_intW11
  wstd_auth01W14 ~~ rcov4*wstd_eu_intW14
  
  
  # Fix the variance and covariance of the random intercepts to 0 
  RIstd_auth01W ~~ 0*RIstd_auth01W
  RIstd_eu_intW ~~ 0*RIstd_eu_intW
  RIstd_auth01W ~~ 0*RIstd_eu_intW

  # Set variances of within-components at first wave to 1
  wstd_auth01W7 ~~ 1*wstd_auth01W7 # Variances
  wstd_eu_intW7 ~~ 1*wstd_eu_intW7 
  
  wstd_auth01W10 ~~ rvx2*wstd_auth01W10 # Residual variances
  wstd_eu_intW10 ~~ rvy2*wstd_eu_intW10 
  wstd_auth01W11 ~~ rvx3*wstd_auth01W11
  wstd_eu_intW11 ~~ rvy3*wstd_eu_intW11 
  wstd_auth01W14 ~~ rvx4*wstd_auth01W14 
  wstd_eu_intW14 ~~ rvy4*wstd_eu_intW14 
  
  # Fix error variance of indicators to (1 - alpha)*var
std_auth01W10 ~~ 0.0058*std_auth01W10
std_auth01W11 ~~ 0.002*std_auth01W11
std_auth01W14 ~~ 0.00725*std_auth01W14
std_eu_intW10 ~~ 0.0028*std_eu_intW10
std_eu_intW11 ~~ 0.00194*std_eu_intW11
std_eu_intW14 ~~ 0*std_eu_intW14

 # Compute correlations of within-components at each wave
  cor2 := a*c + b*d + a*d*cor1 + b*c*cor1 + rcov2
  cor3 := a*c + b*d + a*d*cor2 + b*c*cor2 + rcov3
  cor4 := a*c + b*d + a*d*cor3 + b*c*cor3 + rcov4
 
  # Contrain residual variances of within-components such that variance of each 
  # within-component equals 1
  rvx2 == 1 - (a*a + b*b + 2*a*b*cor1)
  rvy2 == 1 - (c*c + d*d + 2*c*d*cor1)
  rvx3 == 1 - (a*a + b*b + 2*a*b*cor2)
  rvy3 == 1 - (c*c + d*d + 2*c*d*cor2)
  rvx4 == 1 - (a*a + b*b + 2*a*b*cor3)
  rvy4 == 1 - (c*c + d*d + 2*c*d*cor3)
  
    # incorporate tvc - age
wstd_auth01W7 + wstd_eu_intW7 ~ ageW7
wstd_auth01W10 + wstd_eu_intW10 ~ ageW10
wstd_auth01W11 + wstd_eu_intW11 ~ ageW11
wstd_auth01W14 + wstd_eu_intW14 ~ ageW14

ageW7 ~~ ageW7
ageW10 ~~ ageW10
ageW11 ~~ ageW11
ageW14 ~~ ageW14

ageW7 ~~ ageW10 + ageW11 + ageW14
ageW10 ~~ ageW11 + ageW14
ageW11 ~~ ageW14

# incorporate tvc – social grade
wstd_auth01W7 + wstd_eu_intW7 ~ p_socgradeW7
wstd_auth01W10 + wstd_eu_intW10 ~ p_socgradeW10
wstd_auth01W11 + wstd_eu_intW11 ~ p_socgradeW11
wstd_auth01W14 + wstd_eu_intW14 ~ p_socgradeW14

p_socgradeW7 ~~ p_socgradeW7
p_socgradeW10 ~~ p_socgradeW10
p_socgradeW11 ~~ p_socgradeW11
p_socgradeW14 ~~ p_socgradeW14

p_socgradeW7 ~~ p_socgradeW10 + p_socgradeW11 + p_socgradeW14
p_socgradeW10 ~~ p_socgradeW11 + p_socgradeW14
p_socgradeW11 ~~ p_socgradeW14

# incorporate tvc – education
wstd_auth01W7 + wstd_eu_intW7 ~ higheduc7
wstd_auth01W10 + wstd_eu_intW10 ~ higheduc10
wstd_auth01W11 + wstd_eu_intW11 ~ higheduc11
wstd_auth01W14 + wstd_eu_intW14 ~ higheduc14

higheduc7 ~~ higheduc7
higheduc10 ~~ higheduc10
higheduc11 ~~ higheduc11
higheduc14 ~~ higheduc14

higheduc7 ~~ higheduc10 + higheduc11 + higheduc14
higheduc10 ~~ higheduc11 + higheduc14
higheduc11 ~~ higheduc14

# incorporate tvc – income
wstd_auth01W7 + wstd_eu_intW7 ~ incomerec7
wstd_auth01W10 + wstd_eu_intW10 ~ incomerec10
wstd_auth01W11 + wstd_eu_intW11 ~ incomerec11
wstd_auth01W14 + wstd_eu_intW14 ~ incomerec14

incomerec7 ~~ incomerec7
incomerec10 ~~ incomerec10
incomerec11 ~~ incomerec11
incomerec14 ~~ incomerec14

incomerec7 ~~ incomerec10 + incomerec11 + incomerec14
incomerec10 ~~ incomerec11 + incomerec14
incomerec11 ~~ incomerec14
'

std_eu_E_CLPM_W_cov.fit <- lavaan(std_eu_E_CLPM_W_cov, 
                              estimator = "MLR",
                              data = ethnic_dat_white,
                              missing = 'ML', 
                              meanstructure = T, 
                              int.ov.free = T) 
summary(std_eu_E_CLPM_W_cov.fit, standardized = T)

std_eu_E_CLPM_W_noEQ_cov <- '
  # Create between components (random intercepts)
  RIstd_auth01W  =~ 1*std_auth01W7 + 1*std_auth01W10 + 1*std_auth01W11 + 1*std_auth01W14 
  RIstd_eu_intW =~ 1*std_eu_intW7 + 1*std_eu_intW10 + 1*std_eu_intW11 + 1*std_eu_intW14 
  
  # Create within-person centered variables
  wstd_auth01W7 =~ 1*std_auth01W7
  wstd_auth01W10 =~ 1*std_auth01W10
  wstd_auth01W11 =~ 1*std_auth01W11 
  wstd_auth01W14 =~ 1*std_auth01W14

  wstd_eu_intW7 =~ 1*std_eu_intW7
  wstd_eu_intW10 =~ 1*std_eu_intW10
  wstd_eu_intW11 =~ 1*std_eu_intW11
  wstd_eu_intW14 =~ 1*std_eu_intW14

  # Estimate the lagged effects between the within-person centered variables.
  wstd_auth01W10 + wstd_eu_intW10 ~ wstd_auth01W7 + wstd_eu_intW7
  wstd_auth01W11 + wstd_eu_intW11 ~ wstd_auth01W10 + wstd_eu_intW10
  wstd_auth01W14 + wstd_eu_intW14 ~ wstd_auth01W11 + wstd_eu_intW11

  # Estimate the covariance between the within-person centered variables at the first wave. 
  wstd_auth01W7 ~~ wstd_eu_intW7 # Covariance
  
  # Estimate the covariances between the residuals of the within-person centered variables (the innovations).
  wstd_auth01W10 ~~ wstd_eu_intW10
  wstd_auth01W11 ~~ wstd_eu_intW11
  wstd_auth01W14 ~~ wstd_eu_intW14
  
  
  # Fix the variance and covariance of the random intercepts to 0 
  RIstd_auth01W ~~ 0*RIstd_auth01W
  RIstd_eu_intW ~~ 0*RIstd_eu_intW
  RIstd_auth01W ~~ 0*RIstd_eu_intW

  # Estimate the (residual) variance of the within-person centered variables.
  wstd_auth01W7 ~~ wstd_auth01W7 # Variances
  wstd_eu_intW7 ~~ wstd_eu_intW7 
  wstd_auth01W10 ~~ wstd_auth01W10 # Residual variances
  wstd_eu_intW10 ~~ wstd_eu_intW10 
  wstd_auth01W11 ~~ wstd_auth01W11
  wstd_eu_intW11 ~~ wstd_eu_intW11 
  wstd_auth01W14 ~~ wstd_auth01W14 
  wstd_eu_intW14 ~~ wstd_eu_intW14 
  
  # Fix error variance of indicators to (1 - alpha)*var
std_auth01W10 ~~ 0.0058*std_auth01W10
std_auth01W11 ~~ 0.002*std_auth01W11
std_auth01W14 ~~ 0.00725*std_auth01W14
std_eu_intW10 ~~ 0.0028*std_eu_intW10
std_eu_intW11 ~~ 0.00194*std_eu_intW11
std_eu_intW14 ~~ 0*std_eu_intW14

    # incorporate tvc - age
wstd_auth01W7 + wstd_eu_intW7 ~ ageW7
wstd_auth01W10 + wstd_eu_intW10 ~ ageW10
wstd_auth01W11 + wstd_eu_intW11 ~ ageW11
wstd_auth01W14 + wstd_eu_intW14 ~ ageW14

ageW7 ~~ ageW7
ageW10 ~~ ageW10
ageW11 ~~ ageW11
ageW14 ~~ ageW14

ageW7 ~~ ageW10 + ageW11 + ageW14
ageW10 ~~ ageW11 + ageW14
ageW11 ~~ ageW14

# incorporate tvc – social grade
wstd_auth01W7 + wstd_eu_intW7 ~ p_socgradeW7
wstd_auth01W10 + wstd_eu_intW10 ~ p_socgradeW10
wstd_auth01W11 + wstd_eu_intW11 ~ p_socgradeW11
wstd_auth01W14 + wstd_eu_intW14 ~ p_socgradeW14

p_socgradeW7 ~~ p_socgradeW7
p_socgradeW10 ~~ p_socgradeW10
p_socgradeW11 ~~ p_socgradeW11
p_socgradeW14 ~~ p_socgradeW14

p_socgradeW7 ~~ p_socgradeW10 + p_socgradeW11 + p_socgradeW14
p_socgradeW10 ~~ p_socgradeW11 + p_socgradeW14
p_socgradeW11 ~~ p_socgradeW14

# incorporate tvc – education
wstd_auth01W7 + wstd_eu_intW7 ~ higheduc7
wstd_auth01W10 + wstd_eu_intW10 ~ higheduc10
wstd_auth01W11 + wstd_eu_intW11 ~ higheduc11
wstd_auth01W14 + wstd_eu_intW14 ~ higheduc14

higheduc7 ~~ higheduc7
higheduc10 ~~ higheduc10
higheduc11 ~~ higheduc11
higheduc14 ~~ higheduc14

higheduc7 ~~ higheduc10 + higheduc11 + higheduc14
higheduc10 ~~ higheduc11 + higheduc14
higheduc11 ~~ higheduc14

# incorporate tvc – income
wstd_auth01W7 + wstd_eu_intW7 ~ incomerec7
wstd_auth01W10 + wstd_eu_intW10 ~ incomerec10
wstd_auth01W11 + wstd_eu_intW11 ~ incomerec11
wstd_auth01W14 + wstd_eu_intW14 ~ incomerec14

incomerec7 ~~ incomerec7
incomerec10 ~~ incomerec10
incomerec11 ~~ incomerec11
incomerec14 ~~ incomerec14

incomerec7 ~~ incomerec10 + incomerec11 + incomerec14
incomerec10 ~~ incomerec11 + incomerec14
incomerec11 ~~ incomerec14

'

std_eu_E_CLPM_W_noEQ_cov.fit <- lavaan(std_eu_E_CLPM_W_noEQ_cov, 
                                   estimator = "MLR",
                                   data = ethnic_dat_white,
                                   missing = 'ML', 
                                   meanstructure = T, 
                                   int.ov.free = T) 
summary(std_eu_E_CLPM_W_noEQ_cov.fit, standardized = T)

std_eu_E_CLPM_NW_cov <- '
  # Create between components (random intercepts)
  RIstd_auth01W  =~ 1*std_auth01W7 + 1*std_auth01W10 + 1*std_auth01W11 + 1*std_auth01W14 
  RIstd_eu_intW =~ 1*std_eu_intW7 + 1*std_eu_intW10 + 1*std_eu_intW11 + 1*std_eu_intW14 
  
  # Create within-person centered variables
  wstd_auth01W7 =~ NA*std_auth01W7
  wstd_auth01W10 =~ NA*std_auth01W10
  wstd_auth01W11 =~ NA*std_auth01W11 
  wstd_auth01W14 =~ NA*std_auth01W14

  wstd_eu_intW7 =~ NA*std_eu_intW7
  wstd_eu_intW10 =~ NA*std_eu_intW10
  wstd_eu_intW11 =~ NA*std_eu_intW11
  wstd_eu_intW14 =~ NA*std_eu_intW14

  # Estimate the lagged effects between the within-person centered variables.
  wstd_auth01W10 ~ a*wstd_auth01W7 + b*wstd_eu_intW7
  wstd_eu_intW10 ~ c*wstd_auth01W7 + d*wstd_eu_intW7
  wstd_auth01W11 ~ a*wstd_auth01W10 + b*wstd_eu_intW10
  wstd_eu_intW11 ~ c*wstd_auth01W10 + d*wstd_eu_intW10
  wstd_auth01W14 ~ a*wstd_auth01W11 + b*wstd_eu_intW11
  wstd_eu_intW14 ~ c*wstd_auth01W11 + d*wstd_eu_intW11

  # Estimate the covariance between the within-person centered variables at the first wave. 
  wstd_auth01W7 ~~ cor1*wstd_eu_intW7 # Covariance
  
  # Estimate the covariances between the residuals of the within-person centered variables (the innovations).
  wstd_auth01W10 ~~ rcov2*wstd_eu_intW10
  wstd_auth01W11 ~~ rcov3*wstd_eu_intW11
  wstd_auth01W14 ~~ rcov4*wstd_eu_intW14
  
  
  # Fix the variance and covariance of the random intercepts to 0 
  RIstd_auth01W ~~ 0*RIstd_auth01W
  RIstd_eu_intW ~~ 0*RIstd_eu_intW
  RIstd_auth01W ~~ 0*RIstd_eu_intW

  # Set variances of within-components at first wave to 1
  wstd_auth01W7 ~~ 1*wstd_auth01W7 # Variances
  wstd_eu_intW7 ~~ 1*wstd_eu_intW7 
  
  wstd_auth01W10 ~~ rvx2*wstd_auth01W10 # Residual variances
  wstd_eu_intW10 ~~ rvy2*wstd_eu_intW10 
  wstd_auth01W11 ~~ rvx3*wstd_auth01W11
  wstd_eu_intW11 ~~ rvy3*wstd_eu_intW11 
  wstd_auth01W14 ~~ rvx4*wstd_auth01W14 
  wstd_eu_intW14 ~~ rvy4*wstd_eu_intW14 
  
  # Fix error variance of indicators to (1 - alpha)*var
std_auth01W10 ~~ 0.0058*std_auth01W10
std_auth01W11 ~~ 0.002*std_auth01W11
std_auth01W14 ~~ 0.00725*std_auth01W14
std_eu_intW10 ~~ 0.0028*std_eu_intW10
std_eu_intW11 ~~ 0.00194*std_eu_intW11
std_eu_intW14 ~~ 0*std_eu_intW14

 # Compute correlations of within-components at each wave
  cor2 := a*c + b*d + a*d*cor1 + b*c*cor1 + rcov2
  cor3 := a*c + b*d + a*d*cor2 + b*c*cor2 + rcov3
  cor4 := a*c + b*d + a*d*cor3 + b*c*cor3 + rcov4
 
  # Contrain residual variances of within-components such that variance of each 
  # within-component equals 1
  rvx2 == 1 - (a*a + b*b + 2*a*b*cor1)
  rvy2 == 1 - (c*c + d*d + 2*c*d*cor1)
  rvx3 == 1 - (a*a + b*b + 2*a*b*cor2)
  rvy3 == 1 - (c*c + d*d + 2*c*d*cor2)
  rvx4 == 1 - (a*a + b*b + 2*a*b*cor3)
  rvy4 == 1 - (c*c + d*d + 2*c*d*cor3)
  
      # incorporate tvc - age
wstd_auth01W7 + wstd_eu_intW7 ~ ageW7
wstd_auth01W10 + wstd_eu_intW10 ~ ageW10
wstd_auth01W11 + wstd_eu_intW11 ~ ageW11
wstd_auth01W14 + wstd_eu_intW14 ~ ageW14

ageW7 ~~ ageW7
ageW10 ~~ ageW10
ageW11 ~~ ageW11
ageW14 ~~ ageW14

ageW7 ~~ ageW10 + ageW11 + ageW14
ageW10 ~~ ageW11 + ageW14
ageW11 ~~ ageW14

# incorporate tvc – social grade
wstd_auth01W7 + wstd_eu_intW7 ~ p_socgradeW7
wstd_auth01W10 + wstd_eu_intW10 ~ p_socgradeW10
wstd_auth01W11 + wstd_eu_intW11 ~ p_socgradeW11
wstd_auth01W14 + wstd_eu_intW14 ~ p_socgradeW14

p_socgradeW7 ~~ p_socgradeW7
p_socgradeW10 ~~ p_socgradeW10
p_socgradeW11 ~~ p_socgradeW11
p_socgradeW14 ~~ p_socgradeW14

p_socgradeW7 ~~ p_socgradeW10 + p_socgradeW11 + p_socgradeW14
p_socgradeW10 ~~ p_socgradeW11 + p_socgradeW14
p_socgradeW11 ~~ p_socgradeW14

# incorporate tvc – education
wstd_auth01W7 + wstd_eu_intW7 ~ higheduc7
wstd_auth01W10 + wstd_eu_intW10 ~ higheduc10
wstd_auth01W11 + wstd_eu_intW11 ~ higheduc11
wstd_auth01W14 + wstd_eu_intW14 ~ higheduc14

higheduc7 ~~ higheduc7
higheduc10 ~~ higheduc10
higheduc11 ~~ higheduc11
higheduc14 ~~ higheduc14

higheduc7 ~~ higheduc10 + higheduc11 + higheduc14
higheduc10 ~~ higheduc11 + higheduc14
higheduc11 ~~ higheduc14

# incorporate tvc – income
wstd_auth01W7 + wstd_eu_intW7 ~ incomerec7
wstd_auth01W10 + wstd_eu_intW10 ~ incomerec10
wstd_auth01W11 + wstd_eu_intW11 ~ incomerec11
wstd_auth01W14 + wstd_eu_intW14 ~ incomerec14

incomerec7 ~~ incomerec7
incomerec10 ~~ incomerec10
incomerec11 ~~ incomerec11
incomerec14 ~~ incomerec14

incomerec7 ~~ incomerec10 + incomerec11 + incomerec14
incomerec10 ~~ incomerec11 + incomerec14
incomerec11 ~~ incomerec14
'

std_eu_E_CLPM_NW_cov.fit <- lavaan(std_eu_E_CLPM_NW_cov, 
                               estimator = "MLR",
                               data = ethnic_dat_nonwhite,
                               missing = 'ML', 
                               meanstructure = T, 
                               int.ov.free = T) 
summary(std_eu_E_CLPM_NW_cov.fit, standardized = T)

std_eu_E_CLPM_NW_noEQ_cov <- '
  # Create between components (random intercepts)
  RIstd_auth01W  =~ 1*std_auth01W7 + 1*std_auth01W10 + 1*std_auth01W11 + 1*std_auth01W14 
  RIstd_eu_intW =~ 1*std_eu_intW7 + 1*std_eu_intW10 + 1*std_eu_intW11 + 1*std_eu_intW14 
  
  # Create within-person centered variables
  wstd_auth01W7 =~ 1*std_auth01W7
  wstd_auth01W10 =~ 1*std_auth01W10
  wstd_auth01W11 =~ 1*std_auth01W11 
  wstd_auth01W14 =~ 1*std_auth01W14

  wstd_eu_intW7 =~ 1*std_eu_intW7
  wstd_eu_intW10 =~ 1*std_eu_intW10
  wstd_eu_intW11 =~ 1*std_eu_intW11
  wstd_eu_intW14 =~ 1*std_eu_intW14

  # Estimate the lagged effects between the within-person centered variables.
  wstd_auth01W10 + wstd_eu_intW10 ~ wstd_auth01W7 + wstd_eu_intW7
  wstd_auth01W11 + wstd_eu_intW11 ~ wstd_auth01W10 + wstd_eu_intW10
  wstd_auth01W14 + wstd_eu_intW14 ~ wstd_auth01W11 + wstd_eu_intW11

  # Estimate the covariance between the within-person centered variables at the first wave. 
  wstd_auth01W7 ~~ wstd_eu_intW7 # Covariance
  
  # Estimate the covariances between the residuals of the within-person centered variables (the innovations).
  wstd_auth01W10 ~~ wstd_eu_intW10
  wstd_auth01W11 ~~ wstd_eu_intW11
  wstd_auth01W14 ~~ wstd_eu_intW14
  
  
  # Fix the variance and covariance of the random intercepts to 0 
  RIstd_auth01W ~~ 0*RIstd_auth01W
  RIstd_eu_intW ~~ 0*RIstd_eu_intW
  RIstd_auth01W ~~ 0*RIstd_eu_intW

  # Estimate the (residual) variance of the within-person centered variables.
  wstd_auth01W7 ~~ wstd_auth01W7 # Variances
  wstd_eu_intW7 ~~ wstd_eu_intW7 
  wstd_auth01W10 ~~ wstd_auth01W10 # Residual variances
  wstd_eu_intW10 ~~ wstd_eu_intW10 
  wstd_auth01W11 ~~ wstd_auth01W11
  wstd_eu_intW11 ~~ wstd_eu_intW11 
  wstd_auth01W14 ~~ wstd_auth01W14 
  wstd_eu_intW14 ~~ wstd_eu_intW14 
  
  # Fix error variance of indicators to (1 - alpha)*var
std_auth01W10 ~~ 0.0058*std_auth01W10
std_auth01W11 ~~ 0.002*std_auth01W11
std_auth01W14 ~~ 0.00725*std_auth01W14
std_eu_intW10 ~~ 0.0028*std_eu_intW10
std_eu_intW11 ~~ 0.00194*std_eu_intW11
std_eu_intW14 ~~ 0*std_eu_intW14

    # incorporate tvc - age
wstd_auth01W7 + wstd_eu_intW7 ~ ageW7
wstd_auth01W10 + wstd_eu_intW10 ~ ageW10
wstd_auth01W11 + wstd_eu_intW11 ~ ageW11
wstd_auth01W14 + wstd_eu_intW14 ~ ageW14

ageW7 ~~ ageW7
ageW10 ~~ ageW10
ageW11 ~~ ageW11
ageW14 ~~ ageW14

ageW7 ~~ ageW10 + ageW11 + ageW14
ageW10 ~~ ageW11 + ageW14
ageW11 ~~ ageW14

# incorporate tvc – social grade
wstd_auth01W7 + wstd_eu_intW7 ~ p_socgradeW7
wstd_auth01W10 + wstd_eu_intW10 ~ p_socgradeW10
wstd_auth01W11 + wstd_eu_intW11 ~ p_socgradeW11
wstd_auth01W14 + wstd_eu_intW14 ~ p_socgradeW14

p_socgradeW7 ~~ p_socgradeW7
p_socgradeW10 ~~ p_socgradeW10
p_socgradeW11 ~~ p_socgradeW11
p_socgradeW14 ~~ p_socgradeW14

p_socgradeW7 ~~ p_socgradeW10 + p_socgradeW11 + p_socgradeW14
p_socgradeW10 ~~ p_socgradeW11 + p_socgradeW14
p_socgradeW11 ~~ p_socgradeW14

# incorporate tvc – education
wstd_auth01W7 + wstd_eu_intW7 ~ higheduc7
wstd_auth01W10 + wstd_eu_intW10 ~ higheduc10
wstd_auth01W11 + wstd_eu_intW11 ~ higheduc11
wstd_auth01W14 + wstd_eu_intW14 ~ higheduc14

higheduc7 ~~ higheduc7
higheduc10 ~~ higheduc10
higheduc11 ~~ higheduc11
higheduc14 ~~ higheduc14

higheduc7 ~~ higheduc10 + higheduc11 + higheduc14
higheduc10 ~~ higheduc11 + higheduc14
higheduc11 ~~ higheduc14

# incorporate tvc – income
wstd_auth01W7 + wstd_eu_intW7 ~ incomerec7
wstd_auth01W10 + wstd_eu_intW10 ~ incomerec10
wstd_auth01W11 + wstd_eu_intW11 ~ incomerec11
wstd_auth01W14 + wstd_eu_intW14 ~ incomerec14

incomerec7 ~~ incomerec7
incomerec10 ~~ incomerec10
incomerec11 ~~ incomerec11
incomerec14 ~~ incomerec14

incomerec7 ~~ incomerec10 + incomerec11 + incomerec14
incomerec10 ~~ incomerec11 + incomerec14
incomerec11 ~~ incomerec14


'

std_eu_E_CLPM_NW_noEQ_cov.fit <- lavaan(std_eu_E_CLPM_NW_noEQ_cov, 
                                    estimator = "MLR",
                                    data = ethnic_dat_nonwhite,
                                    missing = 'ML', 
                                    meanstructure = T, 
                                    int.ov.free = T) 
summary(std_eu_E_CLPM_NW_noEQ_cov.fit, standardized = T)

# Results 

standardizedsolution(std_eu_E_CLPM_W_cov.fit)
standardizedsolution(std_eu_E_CLPM_NW_cov.fit)
standardizedsolution(std_eu_E_CLPM_W_noEQ_cov.fit)
standardizedsolution(std_eu_E_CLPM_NW_noEQ_cov.fit)

